mongodb的model名称细节

栏目: 数据库 · 发布时间: 6年前

内容简介:最近研究api设计,顺便研究了下mongodb,教程没有仔细看过,所以使用过程中也遇到了一些诡异的现象。比如我使用中发现,我创建的模型名称,在对应数据库的collections内的名称不一致。我很纳闷,比如我创建的如下:当我将一条数据写入后,用工具Robo 3T发现,名称居然变成了people。

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

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

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

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

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

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.

一般情况他会创建一个复数的model,这种person算特殊的了。所以你写得model不一定在查询的时候会一样,即便不一样也不要惊讶哦~


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volume 4,  Fascicle 3

The Art of Computer Programming, Volume 4, Fascicle 3

Donald E. Knuth / Addison-Wesley Professional / 2005-08-05 / USD 19.99

Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 4, Fascicle 3》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具