Node.js 带有聚合管道的Mongoose updateMany

Node.js 带有聚合管道的Mongoose updateMany,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,MongoDB支持使用聚合管道()进行更新。 下面是示例查询()。 update函数将数组(管道)作为第二个参数 然而,在Mongoose中,事实证明您只能传递一个对象(根据文档,我自己也尝试过,不支持数组)。在Mongoose中有什么方法可以使用聚合管道的更新吗?Mongoose还没有为此实现方便的包装器 但是,提供从本机驱动程序对基础集合对象(包括其方法)的只读访问 尝试使用Model.collection.updateMany(),而不是Model.updateMany(),它应该使用Mo

MongoDB支持使用聚合管道()进行更新。 下面是示例查询()。 update函数将数组(管道)作为第二个参数


然而,在Mongoose中,事实证明您只能传递一个对象(根据文档,我自己也尝试过,不支持数组)。在Mongoose中有什么方法可以使用聚合管道的更新吗?

Mongoose还没有为此实现方便的包装器

但是,提供从本机驱动程序对基础集合对象(包括其方法)的只读访问

尝试使用
Model.collection.updateMany()
,而不是
Model.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'
}

如果可能,请通过查询添加样本数据@TusharGupta curioustushar请检查、编辑并添加到mongoplayground的链接。谢谢。你能添加预期的输出详细信息吗?@TusharGupta curioustushar在那里(使用原生MongoDB),它可以按预期工作。问题在于Mongoose,如果你检查我与Mongoose文档共享的链接,它会说它只接受一个对象。因此,当我在Mongoose中运行相同的查询时,它并没有更新文档。我已经选中了
Model.collection.updateMany()
。我的IDE将我路由到它的文档()。事实证明,它直接使用MongoDB Node.js驱动程序提供的方法。如您所见,数组参数也不受支持(仅限于文档)。因此,我仍然无法使用该功能。您需要使用MongoDB 4.2或更高版本才能使用管道更新。是的,我使用的是MongoDB 4.4.1版。