mongodb 将阵列添加到现有阵列Mongo

bvjveswy  于 2023-02-03  发布在  Go
关注(0)|答案(1)|浏览(124)

我正在尝试向现有数组中添加更多数组。
目前是这样的:

"lastmsg" : ISODate("2020-01-27T07:23:03.601Z"),
    "_force_collection" : false,
    "metadata" : {
            "cwmpid" : "1804289383",
            "current_time" : "2020-01-27T08:23:02+00:00",
            "DSLInterface_parameter" : "WANDevice.1.",

我有一个python脚本,它使用update_one$set,但是它删除了元数据中的现有数据,并添加了我需要的内容,但是我想保留现有的元数据,并添加新的字段,而不覆盖现有数据。
下面是python脚本的代码片段:

dict = {
                    "metadata": {
                        "loc": {
                            "mark": row["metadata.loc.geo.bs.mark"],
                            "dslam": {
                                "locname": row["metadata.loc.geo.dslam.locname"],
                                "mark": row["metadata.loc.geo.dslam.mark"],
                                "devname": row["metadata.loc.geo.dslam.devname"],
                            },
                            "geo": {
                                "coordinates": [
                                    float(
                                        row["metadata.loc.geo.coordinates.longitude"]
                                    ),
                                    float(row["metadata.loc.geo.coordinates.latitude"]),
                                ],
                                "type": row["metadata.loc.geo.type"],
                            },
                        }
                    }
                }
                criteria = {"cpeid": cpeid}
                res = mycol.update_one(criteria, {"$set": dict})

After running that it overwrites existing data, so new output looks like this:

        "metadata" : {
                "loc" : {
                        "geo" : {
                                "type" : "Point",
                                "coordinates" : [
                                        16.4665492,
                                        43.5076267
                                ]
                        },
                        "dslam" : {
                                "locname" : "",
                                "devname" : "",
                                "mark" : "0"
                        },
                        "mark" : ""
                }
        }
}

我想得到的是:

"lastmsg" : ISODate("2020-01-27T07:23:03.601Z"),
        "_force_collection" : false,
        "metadata" : {
                "cwmpid" : "1804289383",
                "current_time" : "2020-01-27T08:23:02+00:00",
                "DSLInterface_parameter" : "WANDevice.1.",
                "loc" : {
                        "geo" : {
                                "type" : "Point",
                                "coordinates" : [
                                        16.4665492,
                                        43.5076267
                                ]
                        },
                        "dslam" : {
                                "locname" : "",
                                "devname" : "",
                                "mark" : "0"
                        },
                        "mark" : ""
                }
        }
}
pxyaymoc

pxyaymoc1#

@whoami的解决方案起作用了,我不得不对解决方案进行一些编辑:并从dict中删除metadataloc
此答案是由OP Dominik在CC BY-SA 4.0下发布的问题“将阵列添加到现有阵列Mongo”的edit

相关问题