MongoDB-使用Flask的多层嵌套数组Update()

时间:2019-09-21 06:49:56

标签: python arrays mongodb flask pymongo

我正在尝试使用python-Flask更新MongoDB上的数据,但是我发现了一个错误。我已经按照文档说明如何实现它,但是错误仍然存​​在。有人可以帮我解决这个问题吗?

@app.route('/updateData', methods=['POST'])
def updateData():
    dataList = mongo.db.warehouse
    old_Data = {
        "name": "Pulo Gebang Warehouse"
    }
    new_Data = {
        '$push':{"racks":{"rack columns":{"rack objects":{"items":{'$each':[{"index":4, "item":{"SKU": "HD 2179/3",
                      "arrivalDate": "2019-10-22",
                      "brand": "Philips",
                      "maxQty": 30,
                      "name": "Playstatus 10",
                      "qty": 10}}]}}}}}
    }
    dataList.update(old_Data, new_Data)
    return "Update Success!"

这是我的数据库

[
  {
    "floorRacks": [
      {
        "adjacentRackID": "A1",
        "floorColumn": [
          {
            "floorObjects": [
              {
                "index": 0,
                "item": {
                  "SKU": "HD 1179/3",
                  "arrivalDate": "2019-10-22",
                  "brand": "Philips",
                  "maxQty": 30,
                  "name": "Blender Super mewah",
                  "qty": 10
                }
              },
              {
                "index": 1,
                "item": "null"
              }
            ],
            "index": 0
          }
        ]
      }
    ],
    "name": "Pulo Gebang Warehouse",
    "racks": [
      {
        "code": "A",
        "rack_columns": [
          {
            "columnID": 0,
            "rack_objects": [
              {
                "items": [
                  {
                    "index": 0,
                    "item": {
                      "SKU": "HD 1179/3",
                      "arrivalDate": "2019-10-22",
                      "brand": "Philips",
                      "maxQty": 30,
                      "name": "Blender Super mewah",
                      "qty": 10
                    }
                  },
                  {
                    "index": 1,
                    "item": {
                      "SKU": "HD 1179/3",
                      "arrivalDate": "2019-10-22",
                      "brand": "Philips",
                      "maxQty": 30,
                      "name": "Blender Super mewah",
                      "qty": 10
                    }
                  },
                  {
                    "index": 2,
                    "item": "null"
                  },
                  {
                    "index": 3,
                    "item": {
                      "SKU": "HD 1189/3",
                      "arrivalDate": "2019-10-22",
                      "brand": "Philips",
                      "maxQty": 35,
                      "name": "Blender Super mewah Eksklusif",
                      "qty": 10
                    }
                  }
                ],
                "level": 0
              }
            ]
          }
        ]
      }
    ]
  }
]

这是完整的错误消息

  

pymongo.errors.WriteError:美元($)前缀为字段'$ each'   'racks..rack columns.rack objects.items。$ each'不适用于   存储。

1 个答案:

答案 0 :(得分:0)

尝试将查询更改为以下内容:

  {
    "$push": {
      "racks.rack columns.rack objects.items": {
        "$each": [
          {
            "index": 4,
            "item": {
              "SKU": "HD 2179/3",
              "arrivalDate": "2019-10-22",
              "brand": "Philips",
              "maxQty": 30,
              "name": "Playstatus 10",
              "qty": 10
            }
          }
        ]
      }
    }
  }