MongoDB节点驱动程序updateMany按ID筛选

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

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

当省略新的ObjectId对象的数组并且使用ID的普通string时,它不会返回错误,但标识符也不会被推送到instances数组。

 userIdArray = userIdArray.map(id => new ObjectId(id)); this.collection.updateMany({ _id: {$in: userIdArray} }, { $push: {instances: identifier} }) 

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

提前致谢。


这是完整的function:

  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)返回上面的错误

经过长时间的搜寻,我find了答案。 由于在多个进程上运行多租户架构,我的应用程序使用多个MongoDB驱动程序安装(不同的node_modules文件夹)。

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

我所要做的就是从连接到数据库的地方传递ObjectID函数。

希望这有助于他人在未来。