code: function validateDuplicateName(cb) {
      var formId = formData._id;
      var formName = formData.name;
  
      if (!formName) {
        return cb(new Error("No form name passed"));
      }
  
      var query = {};
  
      //If there is a form id, then the query to the form model must exclude the current form id that is being updated.
      if (formId) {
        query.name = formName;
        //Excluding the formId that is being updated.
        query["_id"] = {"$nin": [formId]};
      } else { //Just checking that the form name exists as a form is being created
        query.name = formName;
      }
  
      formModel.count(query, function(err, count) {
        if (err) {
          return cb(err);
        }
  
        //If the number of found forms is > 0, then there is another form with the same name. Do not save the form.
        if (count > 0) {
          return cb(new Error("Form with name " + formName + " already exists found."));
        } else {//No duplicates, can proceed with saving the form.
          return cb();
        }
      });
    }
docs: Validating form duplicate names.