0

I am trying to add more arrays to existing array.

Currently it's like this:

   "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.",

I have python script that uses update_one and $set, but that removes existing data in metadata and adds what I need, but I want to keep already existing metadata and add new fileds without overwriting existing data.

Here is code snippet of python script:

                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" : ""
                }
        }
}

What I am trying to get is this:

       "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" : ""
                }
        }
}
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Dominik
  • 121
  • 9
  • `$set` will replace the entire field with new value, So you need to use `.` to add or update fields in an object !! So you need to do :: `{$set : {'metadata.loc' : dict.metadata.loc}}` Or if `metadata` is an array then you need to use `$push` instead of `$set` – whoami - fakeFaceTrueSoul Mar 10 '20 at 14:24
  • Does this answer your question? [How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one](https://stackoverflow.com/questions/10290621/how-do-i-partially-update-an-object-in-mongodb-so-the-new-object-will-overlay) – whoami - fakeFaceTrueSoul Mar 10 '20 at 14:25
  • 1
    @whoami solution worked for me, I cant express how thankful I am! I tried $addFileds, $push and nothing worked, couldnt find solution for 12h! Awesome dude! – Dominik Mar 10 '20 at 14:42

1 Answers1

0

@whoami's solution worked, I had to edit solution a bit to this: {"$set": {"metadata.loc": dict}} and remove metadata and loc from dict.


This answer was posted as an edit to the question Add array to existing array Mongo by the OP Dominik under CC BY-SA 4.0.

vvvvv
  • 25,404
  • 19
  • 49
  • 81