【问题标题】:Update nested object using positional-filters on different levels - MongoDb update使用不同级别的位置过滤器更新嵌套对象 - MongoDb 更新
【发布时间】: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


    【解决方案1】:

    你快到了:)

    只是问题在于最后一个数组过滤器和$set

    我已更新查询:

    db.session.update(
        {},
        {
            $set: {
                "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"
                        ]
                    }
                }
            ]
        }
    )   
    

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-24
      • 2023-04-03
      • 2021-02-13
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      相关资源
      最近更新 更多