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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With