0

I want to update every element in the array days (I got 7 days, here I only show 3) with the MongoDB positional operator $[]. The update correctly pushes an object into every single element of the array meals. However, their Object Id's are the same, and I would like them to be different since I will be mapping them. Any ideas?

    "_id": {
        "$oid": "5db749cf657e1b1e0c3d241c"
    },
    "finished": false,
    "days": [
        {
            "dayName": "Monday",
            "meals": [
                {
                    "_id": {
                        "$oid": "5dba1cde12f2b650e4b4f89d"
                    },
                    "mealName": "Breakfast",
                    "recipes": []
                }
            ]
        },
        {
            "dayName": "Tuesday",
            "meals": [
                {
                    "_id": {
                        "$oid": "5dba1cde12f2b650e4b4f89d"
                    },
                    "mealName": "Breakfast",
                    "recipes": []
                }
            ]
        },
        {
            "dayName": "Wednesday",
            "meals": [
                {
                    "_id": {
                        "$oid": "5dba1cde12f2b650e4b4f89d"
                    },
                    "mealName": "Breakfast",
                    "recipes": []
                }
            ],
        }
    ]
}
  • You can try using [Update Elements By Their Index](https://docs.mongodb.com/manual/reference/operator/update/positional/index.html), since there are only 7 elements in the array (fixed size). Also, you can share the code you had tried. Note that `$[]` is used to update _all_ elements in the array (with something). – prasad_ Oct 31 '19 at 01:50
  • Or, you can try updating array elements by index using this method: [set elements in array](https://docs.mongodb.com/manual/reference/operator/update/set/index.html#set-elements-in-arrays). Note this is different from using the `$` array update operator. – prasad_ Oct 31 '19 at 02:40
  • I think you have to create 7 `ObjectId`s and update each one at its index position using one of the array update techniques. – prasad_ Oct 31 '19 at 02:45
  • @prasad_ thanks for your answer, apparently there's not a known (according to my research) way to do it with ```$[]```, so I ended up performing a classic for loop, which gave me unique $oids: ```for(let i = 0; i < 7; i++){ pushObject[`days.${i}.meals`] = {...otherProperties} }``` – Stefano Martell Nov 05 '19 at 20:52
  • There are some posts online with similar subject, and you have to do some searching for more ideas. Here is one of them: [Creating ObjectId For Each New Child Added To The Array Field](https://stackoverflow.com/questions/35050750/mongodb-creating-an-objectid-for-each-new-child-added-to-the-array-field) – prasad_ Nov 06 '19 at 01:15
  • What is the reason you are using "$oid" for`_id` value? What is [ObjectId](https://docs.mongodb.com/manual/reference/method/ObjectId/index.html) and ["$oid"](https://docs.mongodb.com/manual/reference/mongodb-extended-json/index.html#bson.ObjectId). – prasad_ Nov 06 '19 at 01:20

0 Answers0