gpt4 book ai didi

node.js - MongoDB Node Driver updateMany 按 ID 过滤

转载 作者:可可西里 更新时间:2023-11-01 10:42:01 26 4
gpt4 key购买 nike

我在 Node.JS 中遇到 MongoDB 驱动程序问题。当尝试通过 ID 更新多个用户时,MongoDB 返回错误:

{"name":"MongoError","message":"Argument must be a string"}

当省略新 ObjectId 对象的数组并使用普通的 ID 字符串时,它不会返回任何错误,但标识符也不会被推送到实例数组。

userIdArray = userIdArray.map(id => new ObjectId(id));

this.collection.updateMany({
_id: {$in: userIdArray}
}, {
$push: {instances: identifier}
})

当直接从 Mongo Shell 中使用 ObjectId 数组执行此 updateMany 函数时,查询成功运行。

提前致谢。


这是完整的函数:

  this.collection.updateMany({
_id: {$in: userIdArray}
}, {
$push: {instances: identifier}
}).then((result) => {
callback(null, 'successfully added instance to users.');
}, (err) => {
console.log(err);
callback(err);
}).catch((err) => {
new Report().error(err);
})

console.log(err) 返回上面的错误

最佳答案

经过长时间的搜索,我想通了。由于其在多个进程上运行的 Multi-Tenancy 架构,我的应用程序使用多个 MongoDB 驱动程序安装(不同的 node_modules 文件夹)。

我使用一个安装的数据库连接和另一个安装的 ObjectID 函数,方法是:require('mongodb').ObjectID;

我所要做的就是从连接到数据库的同一位置传递 ObjectID 函数。

希望这对以后的其他人有帮助。

关于node.js - MongoDB Node Driver updateMany 按 ID 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com