node.js - 使用 arrayFilters 的 mongoDB 更新不起作用
问题描述
我在 mongo 中有以下文档结构(简化):
{
"_id":"5e30208675b5400cb0894c52",
"locations": [
{
"name": "Pleasure Gardens",
"id": 618,
"areas": [
{
"name": "Koi Pond",
"area_id": 159,
"is_active": true,
},
...other areas
],
...other locations
}
]
}
我正在尝试将区域数组中的is_active字段设置为 false,其中位置的 id 为 618,区域的 area_id 为 159。
我正在执行以下操作:
const db = await connectDb();
return await db
.collection('test')
.updateOne(
{"locations.areas.area_id": 159},
{ $set: {"locations.$[l].areas.$[a].is_active": false }},
{ arrayFilters: [{ "l.id": 618 }, { "a.area_id": 159 }] }
);
但我得到:
MongoError: cannot use the part (locations of locations.$[l].areas.$[a].is_active) to traverse the element
我已经检查了其他响应,据我所知语法是正确的,而且我还在使用支持 nodejs 中的 arrayFilters 方法(“mongodb”:“^3.6.3”)的 mongodb 版本,所以为什么会这样对我这么残忍?为什么?!?!
更新
看完评论才发现虽然我安装了3.6.3版本的mongodb npm包,但是数据库本身的版本其实是3.0.14
这意味着不支持 arrayFilters 方法,我可能不得不采取两步方法来解决这个问题:
- 查询 mongodb 查找文档:
- 将结果存储在变量中
- 更新代码本身中的变量(将 is_active 更改为 false)
- 使用更新查询将 mongo 中的整个文档替换为存储在变量中的文档(而不仅仅是更新所需区域)
解决方案
对我来说工作正常(mongod/mongos 4.4.3 版):
mongos> db.l.find().pretty()
{
"_id" : "5e30208675b5400cb0894c52",
"locations" : [
{
"name" : "Pleasure Gardens",
"id" : 618,
"areas" : [
{
"name" : "Koi Pond",
"area_id" : 159,
"is_active" : true
}
]
}
]
}
mongos> db.l.updateOne( {"locations.areas.area_id": 159} , { $set:{"locations.$[loc].areas.$[are].is_active":false }}, { arrayFilters: [{ "loc.id": 618 }, { "are.area_id": 159 }] } )
// { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
mongos> db.l.find({ "locations.areas.area_id": 159 } ,{"locations.areas.is_active":1 })
// { "_id" : "5e30208675b5400cb0894c52", "locations" : [ { "areas" : [ { "is_active" : false } ] } ] }
推荐阅读
- sql - 无法确定连接查询
- python - MyPy 无法识别修饰函数的返回类型
- javascript - Javascript - 通过其名称和存储在变量中的参数调用函数,(理想情况下没有`eval`,但如果只有这样就可以了)
- python - Discord.py 如何让命令忽略参数
- java - 实现抽象类的类的类型与抽象类的泛型类型不兼容
- args - For循环不循环通过args
- python - 尝试与 JMP 中的 Python 脚本交互
- php - 变量“可能未定义” - 我错过了什么吗?
- python - pandasql 在函数中不起作用
- strlen - strlen (php) 没有给出正确的结果