0

i'm trying to update data on MongoDB using python-Flask but i found an error. I have followed the documentation how to implement it but the error still there. Could anyone help me to fix this?

@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!"

This is my Database

[
  {
    "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
              }
            ]
          }
        ]
      }
    ]
  }
]

and here is the full ERROR Message

pymongo.errors.WriteError: The dollar ($) prefixed field '$each' in 'racks..rack columns.rack objects.items.$each' is not valid for storage.

Akhdan Rasiq
  • 83
  • 1
  • 6

1 Answers1

0

Try changing the query to below:

  {
    "$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
            }
          }
        ]
      }
    }
  }
sushant mehta
  • 1,244
  • 1
  • 7
  • 13
  • it says : pymongo.errors.WriteError: Cannot create field 'rack columns' – Akhdan Rasiq Sep 21 '19 at 07:01
  • can you save fieldName as rack_columns instead of rack columns and try to replace keys that have spaces – sushant mehta Sep 21 '19 at 07:16
  • it still says: pymongo.errors.WriteError: Cannot create field 'rack_columns' – Akhdan Rasiq Sep 21 '19 at 07:25
  • i update my question above along with my mongo database, please check it, mybe it can help – Akhdan Rasiq Sep 21 '19 at 07:31
  • you have mutli level nested arrays that' why update not working you need to use arrayFIlters or positional operator for same https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/ https://docs.mongodb.com/manual/reference/operator/update/positional/ – sushant mehta Sep 21 '19 at 07:52