如何在MongoDB中的嵌入式数组对象内插入json对象?

问题描述 投票:0回答:1

我在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个回答
0
投票

这可以使用更新查询来完成。以下是使用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
            }
        }]
    }}})
© www.soinside.com 2019 - 2024. All rights reserved.