mongoose 常见问题
2017-05-04 本文已影响33人
千罹
mongoose 会自动把表名变成复数
log => logs
reply => replies
原因:
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)时,默认会给你变成复数的名字,当你不喜欢它变成复数的时候,需要加上第三个参数
var schema = new Schema({ name: String }, { collection: 'actor' });
// or
schema.set('collection', 'actor');
// or
var collectionName = 'actor'
var M = mongoose.model('Actor', schema, collectionName)