2

I am trying to replace/update a whole object in an array to it's latest values, but I cannot get it to work.

Db looks like this: (Note: there is only 1 main object in this collection)

    {
        "_id": {...},
        "something that doesnt matter": {...},
        "var1": {
            "var2": [{...}, {...}, {...}, {...}, {...}],
            "var3": [{...}, {...}, {...}, {...}, {...}]
        },
        "something that doesnt matter": {...}
    }

I need to update a certain object from array var2, I have the object ID or there is a custom ID in the object that I can also get it with (id == updatedObject.id)

This worked but I cannot get it to work with a custom array id

await db.collection("collectionName").findOneAndUpdate(
    {"var1.var2": { $exists: true }},
    { $set: { "var1.var2.1": updatedObject } }
);

I have the ID of the object already in the array on the db, but idk how to update it from var1.var2.ID,

so basically what I need is { $set: { "var1.var2.**ID**": updatedObject } } but I cant seem to find out how to get it to work.

Cause I dont want to update the whole array, and I also dont want to update a single variable in the object. I need to update the whole object.

Thank you in advance for your replies.

Bas950
  • 47
  • 1
  • 6
  • Have a look at this Question, https://stackoverflow.com/questions/47225132/update-nested-subdocuments-in-mongodb-with-arrayfilters – Mu-Majid Jul 18 '20 at 18:14
  • Have you searched around for solutions on SO? searching the with the exact title of your question gets this https://stackoverflow.com/questions/9200399/replacing-embedded-document-in-array-in-mongodb, does this help? – thammada.ts Jul 19 '20 at 06:03

2 Answers2

1

Have you tried as below

await db.collection("collectionName").findOneAndUpdate(
  {
    "var1.var2.id": id // id value (or any matching field) of object inside array you want to update
  },
  {
    $set: {
      "var1.var2.$": updatedObject // Update with new object
    }
  }
);

Hope this official mongodb documentation helps better for your requirement.

Prathap Reddy
  • 1,688
  • 2
  • 6
  • 18
  • It always updates the first element with that, even though that isn't the correct element. – Bas950 Jul 18 '20 at 17:54
  • Even after adding the exact `id` match in find query `"var1.var2.id": id`? – Prathap Reddy Jul 18 '20 at 17:55
  • http://prntscr.com/tkb05f as you can see there I even did it with string to make sure it was correct but still it always does the first element in the array. – Bas950 Jul 18 '20 at 18:10
  • Before doing any update, are you able to find that object inside array with `.find` at first? You can keep the same find criteria here in you `query` for `.findAndUpdateOne`. I gave this `"var1.var2.id": id` as a sample. You can update with any find criteria per say. – Prathap Reddy Jul 18 '20 at 18:15
  • 1
    yerp it finds it, it returns the whole object ofc – Bas950 Jul 18 '20 at 18:28
  • > I gave this "var1.var2.id": id as a sample. You can update with any find criteria per say. that is the only id that will always stay the same in the object. But I got the index of where the object is in the array and the id inside of the object. – Bas950 Jul 18 '20 at 18:31
  • Can you please try removing `"var1.var2": { $exists: true }` line in my answer and keep only `"var1.var2.id": id` in find criteria? I could only think of this option based on the question info provided and as per my knowledge. Please do not forget to update your answer once you find it. – Prathap Reddy Jul 18 '20 at 18:43
  • 1
    :clap: :clap: That did the trick! await db.collection("collectionName").findOneAndUpdate( {"var1.var2.id": id}, { $set: { "var1.var2.$": updatedObject } } ); – Bas950 Jul 18 '20 at 18:47
  • Lol... It's a little guess though. Let me update my answer. – Prathap Reddy Jul 18 '20 at 18:48
0

Sorry, I'm not able to comment but the above answer is almost correct except that you have to filter by var1.var2._id instead of var1.var2.id because mongodb default ID field is _id

Neoflies
  • 263
  • 1
  • 3
  • 7