这两天折腾mongoose,发现数据成功写入集合了,但是在Terminal查询的时候却查不到
于是show collections后发现在原来的集合底下,又生成了一个加了s的集合,shenmegui

查了一下,发现是mongoose.model()的问题

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

Mongoose#model(name, [schema], [collection], [skipInit])

在官方的api文档里面有解释

When no collection argument is passed, Mongoose produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. If you don’t like this behavior, either pass a collection name or set your schemas collection name option.

 

当没有传入collection参数时,Mongoose会通过model name(就是第一个参数),调用utils.toCollectionName方法产生一个collection name,而这个方法会使name变成复数形式。如果你不想要这个过程,只要传入collection name参数或设置Schema中的collection name选项。

就像这样:

var schema = new Schema({ name: String }, { collection: 'actor' });

// or

schema.set('collection', 'actor');

// or

var collectionName = 'actor'
var M = mongoose.model('Actor', schema, collectionName);



扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄