Mongoose 还没有为此实现方便的包装器。
但是,Model.collection提供了从本机驱动程序对基础集合对象的只读访问,包括其方法。
而不是Model.updateMany(),尝试使用Model.collection.updateMany(),它应该使用为 MongoDB Node.JS 驱动程序描述的语法
编辑
使用 Mongoose 5.12 和 MongoDB 4.2 的丑陋演示代码:
const mongoose=require('mongoose');
const testSchema = new mongoose.Schema({ test:String });
const testModel = mongoose.model('testModel',testSchema);
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true, useUnifiedTopology: true});
async function main() {
const doc = new testModel({test:"testString"});
await doc.save();
const result = await testModel.collection.updateMany({},[{$set:{updated:{$concat:["$test",{$toString:"$_id"}]}}}]);
console.log("matchedCount:",result.matchedCount);
console.log("modifiedCount:",result.modifiedCount);
const newdoc = await testModel.findOne({});
console.log(newdoc);
process.exit();
}
main();
上述输出:
matchedCount: 1
modifiedCount: 1
{
_id: 606945d4d5a5d9bec9038a59,
test: 'testString',
__v: 0,
updated: 'testString606945d4d5a5d9bec9038a59'
}