Model name details of mongodb

Time:2020-1-14

Recently, I studied the API design and mongodb by the way. I didn’t read the tutorial carefully, so I also encountered some strange phenomena in the use process.

For example, I found that the name of the model I created is inconsistent in the corresponding database collections. I wonder, for example, what I created is as follows:

const PersonModel = Mongoose.model("person", {
  firstname: String,
  lastname: String
});

When I write a piece of data, I use the tool Robo 3T to find that the name actually becomes people.

Later, I checked the relevant information. It turns out that mongodb has its own set of rules, detailed rules, such as my one: mongoose / lib / utils.js. Of course, this is an example of the historical version. As for this phenomenon, the latest document also points out:

The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural version of your model name. For example, if you use

const MyModel = mongoose.model('Ticket', mySchema);

Then Mongoose will create the model for your tickets collection, not your ticket collection.

In general, he will create a complex number of models. This person is special. So you don’t have to write a model that will be the same when querying. Don’t be surprised if it’s not the same~