Update DB object from an array of objects

I have the following collection:

{
    "_id" : "ak5TEry8rzRjxDqWJ",
    "client" : "Client Name",
    "title" : "Product Title",
    "category" : "box",
    "timestamp" : 1497460110992.0,
    "order" : [ 
        {
            "date" : 1497375153945.0,
            "brief" : {
                "item" : {
                    "text" : "item order 0",
                    "show" : false
                },
                "quantity" : {
                    "text" : "edit",
                    "show" : false
                }
            },
            "dev" : {
                "quote" : {
                    "text" : "edit",
                    "show" : false
                },
                "notes" : {
                    "text" : "edit",
                    "show" : false
                }
            }
        }
    ]
}

Inside the array order I will have as many objects as reorders.

How can I update item from brief inside order.array[x]

x is a variable with the element number of the array

I’ve tried to write an update query but failed.

I’m close to giving up and rewrite the DB with an object of objects but does any of you have a solution?

I’d look at your general structure first. Adding an unknown number of embedded docs seems like a recipe for trouble. Why not create a new doc for each reorder and then add it as a reference somewhere else?

Have you checked the mongodb docs? You should be able to

Collection.update("ak5TEry8rzRjxDqWJ",{
 $set:{
  "order.0": {
    brief: { item: {text: "item order 0",  show: true } }
   }
  }
 }
})

Or even

Collection.update("ak5TEry8rzRjxDqWJ",{
 $set:{
  "order.0.brief.item.show": true
 }
})

If you put a unique identifier on each object inside of the order array, you should be able to use

{order.$.id: myId}

to do the document identification, and then update with

{$set: {'order.$.brief.item.text': 'new text'}}

IIRC this is how that works. I implemented something like this a year ago but could’t find an example.

There is documentation on it somewhere in the Mongo docs. As @jamgold pointed out, make sure to read through them.

https://forums.meteor.com/t/update-nested-array-in-collection/37450