【问题标题】:How to insert a json object inside embedded array object in MongoDB?如何在 MongoDB 的嵌入式数组对象中插入 json 对象?
【发布时间】:2020-06-07 20:39:58
【问题描述】:

我在 MongoDB 中存储了以下文档:

{
    "_id": 2,
    "template_name": "AllahabadBank",
    "description": "Form For NewCustomer Application In Banking System",
    "handwritten": "true",
    "file_path": "./serverData/batchProcessedFiles/AllahabadBank/input",
    "annotated_data": [{
        "page_num": "page-01.jpg",
        "entities": [{
            "label": "CIFNo",
            "type": "text",
            "boundary_box": {
                "x1": 325,
                "x2": 861,
                "y1": 324,
                "y2": 360
            }
        }],
    "num_of_pages": 12
  }]
}

并且想将下面的 JSON 数据插入到 annotated_data。建议我使用 MongoDB 查询或 Python 代码来执行此操作。

{
  "page_num": "page-02.jpg",
  "entities": [
    {
      "label": "CustomerName",
      "type": "text",
      "boundary_box": {
        "x1": 559,
        "x2": 1615,
        "y1": 382,
        "y2": 440
      }
    }
  ]
}

【问题讨论】:

  • 您的示例文档无效。请提供一个有效的。

标签: python-3.x mongodb nosql pymongo


【解决方案1】:

这可以使用更新查询来完成。 下面是使用 pymongo 更新它的 python 代码。将 __ colName __ 替换为您的列名。

__colName__.update({'_id':2},
            {'$push':{'annotated_data':{"page_num": "page-02.jpg",
        "entities": [{
            "label": "CustomerName",
            "type": "text",
            "boundary_box": {
                "x1": 559,
                "x2": 1615,
                "y1": 382,
                "y2": 440
            }
        }]
    }}})

上面的Mongo命令如下:

db.__colName__.update({'_id':2},
            {'$push':{'annotated_data':{"page_num": "page-02.jpg",
        "entities": [{
            "label": "CustomerName",
            "type": "text",
            "boundary_box": {
                "x1": 559,
                "x2": 1615,
                "y1": 382,
                "y2": 440
            }
        }]
    }}})

【讨论】:

    【解决方案2】:

    您可以通过两种方式做到这一点:

    1) MongoDB更新方法

    db.collection.update({"_id":2},
      {
      $push: {
        "annotated_data": {
          "page_num": "page-02.jpg",
          "entities": [
            {
              "label": "CustomerName",
              "type": "text",
              "boundary_box": {
                "x1": 559,
                "x2": 1615,
                "y1": 382,
                "y2": 440
              }
            }
          ]
        }
      }
    })
    

    2) 将文档和append 检索到数组中(Python 代码)

    doc = db.collection.find_one({'_id':2})
    doc["annotated_data"].append({
      "page_num": "page-02.jpg",
      "entities": [
         {
           "label": "CustomerName",
           "type": "text",
           "boundary_box": {
              "x1": 559,
              "x2": 1615,
              "y1": 382,
              "y2": 440
           }
        }
      ]
    })
    db.collection.save(doc)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-25
      • 2015-08-13
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 2016-08-21
      • 1970-01-01
      相关资源
      最近更新 更多