Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the relevance of first string parameter in mongoose.model method?

while compiling a schema we use the mongoose.model, I am not able to understand the relevance of first String argument accepted by the model method

var Kitten = mongoose.model('Kitten', kittySchema);

Here the first argument inside model method is 'Kitten' and the second argument is the schema, can I give any name to the first argument?

its not clear in the docs http://mongoosejs.com/docs/api.html#model-js

Am I referring the correct docs?

like image 544
MeanMan Avatar asked Dec 13 '25 07:12

MeanMan


2 Answers

We need to convert our Schema into a Model we can work with. To do so, we pass it into mongoose.model(modelName, schema):

so you can use any name that you want for modelName but it should be meaning full

also valid if you define userSchema as model name User or Customer like:

mongoose.model('User', userSchema); then table will be creat with name users

or

mongoose.model('Customer', userSchema); then table will be creat with name customers

see this document and this one

like image 168
Shaishab Roy Avatar answered Dec 16 '25 06:12

Shaishab Roy


In your example:

var Kitten = mongoose.model('Kitten', kittySchema);

The difference between the variable name Kitten and the parameter string "Kitten" is that kitten refers to the model name, and "Kitten" refers to the collection name.

NOTE: mongodb will convert the string "Kitten" into a plural form, so you will find the collection named "Kittens" (with 's' at the end) inside your mongodb.

like image 24
Fouad Boukredine Avatar answered Dec 16 '25 07:12

Fouad Boukredine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!