Search code examples
node.jsmongoosemongoose-schema

what is the difference between these 2 mongoose schemas while exporting them?


Below is the schema for one user profile in my project.

 var agencyProfile = mongoose.Schema({
      name: {
        type: String
      },
      user: {
        type: mongoose.Schema.ObjectId,
        ref: "users"
      }
    })

I would like to know what is the difference between these 2 exported schemas?

 module.exports = mongoose.model('agencyProfile', agencyProfile);
    vs
module.exports = mongoose.model('agencyProfile', agencyProfile, "agencyProfile");

Solution

  • The third argument basically allows you to change the collection name (which is inferred from the model-name by default) to something else. From the documentation:

    When no collection argument is passed, Mongoose uses the model name. If you don't like this behavior, either pass a collection name, use mongoose.pluralize(), or set your schemas collection name option.

    In your case, it does not make any difference as the collection name matches the model name agencyProfile.