【发布时间】:2020-04-13 14:52:43
【问题描述】:
考虑到我有以下集合(忽略文档_id):
收藏
[
{
"Id": "1",
"Level2": [
{
"Id": "1.1",
"Level3": [
{
"Id": "1.1.1",
"Level3_4": {
"Transport": "Car",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "run",
"Direction": "N"
},
{
"Id": "1.1.1.2",
"Status": "run",
"Direction": "S"
},
{
"Id": "1.1.1.3",
"Status": "pause",
"Direction": "S"
},
{
"Id": "1.1.1.4",
"Status": "run",
"Direction": "W"
}
]
}
},
{
"Id": "1.1.2",
"Level3_4": {
"Transport": "Bus",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "run",
"Direction": "S"
},
{
"Id": "1.1.2.2",
"Status": "stop",
"Direction": "N"
},
{
"Id": "1.1.2.3",
"Status": "stop",
"Direction": "W"
},
{
"Id": "1.1.2.4",
"Status": "run",
"Direction": "E"
}
]
}
}
]
},
{
"Id": "1.2",
"Level3": [
{
"Id": "1.2.1",
"Level3_4": {
"Transport": "Train",
"Level4": [
{
"Id": "1.2.1.1",
"Status": "run",
"Direction": "N"
}
]
}
},
{
"Id": "1.2.2",
"Level3_4": {
"Transport": "Bus",
"Level4": []
}
}
]
}
]
}
]
更新
我想更新集合中的所有文档,例如:
Level2.Level3.Level3_4.Level4.Status: "pause"
使用 updateOne 并且不使用 upsert
所有符合下一个条件的Level4
条件
地点:
-
Level2.Level3.Id $in ["1.1.1","1.1.2"]与 -
Level2.Level3.Level3_4.Level4.Status $in ["run", "stop"]与 Level2.Level3.Level3_4.Level4.Direction $in ["N", "S"]
结果
结果集合应该是:
[
{
"Id": "1",
"Level2": [
{
"Id": "1.1",
"Level3": [
{
"Id": "1.1.1",
"Level3_4": {
"Transport": "Car",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "pause",
"Direction": "N"
},
{
"Id": "1.1.1.2",
"Status": "pause",
"Direction": "S"
},
{
"Id": "1.1.1.3",
"Status": "pause",
"Direction": "S"
},
{
"Id": "1.1.1.4",
"Status": "run",
"Direction": "W"
}
]
}
},
{
"Id": "1.1.2",
"Level3_4": {
"Transport": "Bus",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "pause",
"Direction": "S"
},
{
"Id": "1.1.2.2",
"Status": "pause",
"Direction": "N"
},
{
"Id": "1.1.2.3",
"Status": "stop",
"Direction": "W"
},
{
"Id": "1.1.2.4",
"Status": "run",
"Direction": "E"
}
]
}
}
]
} ...
]
}
]
我正在努力使用 $set 运算符和 arrayFilters。我怎样才能做到这一点?
这是我目前为止的目标:
db.test.update({},
{
$set: {
"Level2.$[level2].Level3.$[level3].Level3_4.Level4.$[level4].Status": "pause"
}
},
{
arrayFilters: [
{
"level3.Id": {
$in: [
"1.1.1",
"1.1.2"
]
}
},
{
"level4.Status": {
$in: [
"run",
"stop"
]
}
},
{
"level4.Direction": {
$in: [
"N",
"S"
]
}
}
]
}
)
但是它不起作用......我做错了什么?
【问题讨论】:
标签: arrays mongodb mongodb-query aggregation-framework pymongo