'How to update specific objects properties within an array in mongoose

I'm trying to update objects within an array in a document. The objects are not subdocuments and therefore don't have a unique ID.

Here's an example of a document in this collection:

{
        "_id": "3e4f81a5-4a51-4acd-a243-9ca3359a41be",
        "items_to_update": [
            {
              "_id": {
                "$oid": "6230823a0507b80d28b054e2"
              },

              es: {
                 "key": "Key1",
                 "value": "V1"
              },
              en: {
                 "key": "[EN] Key1",
                 "value": "[EN] V1"
              }
            },
            {
              "_id": {
                "$oid": "6230823a0507b80d28b054e3"
              },
              es: {
                 "key": "Key2",
                 "value": "V2"
              },
              en: {
                 "key": "[EN] Key2",
                 "value": "[EN] V2"
              }
            }
          ],
}

The idea is to receive an array with values for one "locale key". For example, I'll receive the following object:

const newValueForEnglishKeys = [{key: "[EN] Key1 bis", value: "[EN] V1 bis"}, {key: "[EN] Key2 bis", value: "[EN] V2 bis"}]

And i should only update the corresponding key of every object in the array (I'll also receive a "Locale" value as parameter such as "en" or "es").

The final document should be:

{
        "_id": "3e4f81a5-4a51-4acd-a243-9ca3359a41be",
        "items_to_update": [
            {
              "_id": {
                "$oid": "6230823a0507b80d28b054e2"
              },

              es: {
                 "key": "Key1",
                 "value": "V1"
              },
              en: {
                 "key": "[EN] Key1 bis",
                 "value": "[EN] V1 bis"
              }
            },
            {
              "_id": {
                "$oid": "6230823a0507b80d28b054e3"
              },
              es: {
                 "key": "Key2",
                 "value": "V2"
              },
              en: {
                 "key": "[EN] Key2 bis",
                 "value": "[EN] V2 bis"
              }
            }
          ],
}

I don't know if it is even possible, can somebody give me at least a clue?



Solution 1:[1]

In this case where you don't know what properties/values may come from your parameter object, you will need to query db to get a fresh document instance, then you will merge the properties and finally will send an update will the entire new doc

const doc = await myModel.findById('my-id).exec()
doc.items_to_update = [ { .... }] //set your values here

//update doc in db
await myModel.update(
  {_id: 'my-id'}, 
  {$set: doc}).exec();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jone Polvora