mongodb的model名称细节

最近研究api设计,顺便研究了下mongodb,教程没有仔细看过,因此使用过程当中也遇到了一些诡异的现象。javascript

好比我使用中发现,我建立的模型名称,在对应数据库的collections内的名称不一致。我很纳闷,好比我建立的以下:java

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

当我将一条数据写入后,用工具Robo 3T发现,名称竟然变成了people。git

后来查了相关资料,原来mongodb有本身的一套规则,详细的规则,好比我这条:mongoose/lib/utils.js。固然这个是历史版本的例子了。关于这个现象,最新文档中也指出:github

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 usemongodb

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

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

通常状况他会建立一个复数的model,这种person算特殊的了。因此你写得model不必定在查询的时候会同样,即使不同也不要惊讶哦~api

相关文章
相关标签/搜索