猫鼬复数集合定义
Mongoose plural collection definition
我正在尝试根据特定 id
创建不同的集合,我这样做的方式是:
db.save({...}, 'collection_' + id).then(function() {
deferred.resolve();
});
这很好用,我可以在我的数据库中看到新的集合。
但是,当我尝试加载此数据时,它不起作用,因为 mongoose 强制它是复数的。
var Items = mongoose.model('collection_' + id, MySchema);
Items.count(function(err, count) {
console.log(count); = 0
});
id
是完全动态的,并且会根据我提出的每个请求而变化,我怎样才能阻止 mongoose
向每个集合名称添加 's'?
您可以如下显式设置模型的集合名称:
var collectionName = 'collection_' + id;
var Items = mongoose.model(collectionName, MySchema, collectionName);
// ^Change this if you want ^This sets the collection name
查看此 answer 以了解更多设置集合名称的方法。
我正在尝试根据特定 id
创建不同的集合,我这样做的方式是:
db.save({...}, 'collection_' + id).then(function() {
deferred.resolve();
});
这很好用,我可以在我的数据库中看到新的集合。
但是,当我尝试加载此数据时,它不起作用,因为 mongoose 强制它是复数的。
var Items = mongoose.model('collection_' + id, MySchema);
Items.count(function(err, count) {
console.log(count); = 0
});
id
是完全动态的,并且会根据我提出的每个请求而变化,我怎样才能阻止 mongoose
向每个集合名称添加 's'?
您可以如下显式设置模型的集合名称:
var collectionName = 'collection_' + id;
var Items = mongoose.model(collectionName, MySchema, collectionName);
// ^Change this if you want ^This sets the collection name
查看此 answer 以了解更多设置集合名称的方法。