mongoose连接collection后自动加s的问题

mongoose连接collection后自动加s的问题

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

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

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选项。

就像这样:

12345678910var schema = new Schema({ name: String }, { collection: 'actor' });// orschema.set('collection', 'actor');// orvar collectionName = 'actor'var M = mongoose.model('Actor', schema, collectionName);

什么坑爹设定…

参考

Mongoose API v4.5.9

🌸 相关推荐

CNTV 世界杯 手机电视 无线互联网
家庭版microsoft365

CNTV 世界杯 手机电视 无线互联网

📅 07-07 👀 6039
从零开始DIY一辆蓝牙遥控小车
365提前结束投注

从零开始DIY一辆蓝牙遥控小车

📅 06-30 👀 8009
雕鸮能闻到将死之人的腐烂气味?攻击谁就是快死了?(误会)