首页 > 解决方案 > Mongoose:updateMany() 没有按预期工作

问题描述

我正在使用猫鼬来处理我的数据库查询。我正在尝试完全使用此方法更新一组记录。模式代码如下所示:

// prepare database query
const filter = { type: 'company' };
const update = req.body.payload; // payload contains the array of objects (i.e. updated records)
const options = { new: true, runValidators: true }

// find and update the taxonomy record
await Taxonomy.updateMany(filter, update, options);

但是,每当我运行此查询时,我都会在控制台中收到以下错误:

Error [MongooseError]: Invalid update pipeline operator: "_id"

我想我的更新有效负载有问题。req.body.payload看起来像这样:

[
    {
        _id: '5ef3d08c745428001d92f896',
        type: 'company',
        name: 'Company Size',
        __v: 0
    },
    {
        _id: '5ef3cdc5745428001d92f893',
        type: 'company',
        name: 'Company Industry',
        __v: 0
    }
]

你能告诉我这里到底出了什么问题吗?

标签: node.jsmongodbmongoose

解决方案


推荐阅读