1

I have a document with nested aggregations and I would like to set the all the attributes full to true. I have tried several methods using $set, but I am not able to update it. How can this be achieved?

{
"_id" : ObjectId("5b4347734e69052e544da67e"),
"providerId" : ObjectId("5b4242b9d8ff1b0020daab34"),
"aggregations" : [ 
    {
        "startTime" : "2012-01-01T00:00:00Z",
        "endTime" : "2012-03-01T00:00:00Z",
        "quantity" : 0,
        "full" : false
    }, 
    {
        "startTime" : "2012-01-01T00:00:00Z",
        "endTime" : "2012-03-01T00:00:00Z",
        "quantity" : 0,
        "full" : false
    }
],
"__v" : 0

}

Ashh
  • 44,693
  • 14
  • 105
  • 132
Ricardo Martin
  • 129
  • 1
  • 8

1 Answers1

0

You can try using $[] The all positional operator which updates multiple documents inside the array

db.collection.update(
  { },
  { "$set": { "aggregations.$[].full": true }}
)

and if you want to increment then

db.collection.update(
  { "aggregations.full": false },
  { "$inc": { "aggregations.$.quantity": 2 }}
)
Ashh
  • 44,693
  • 14
  • 105
  • 132
  • Thanks, problem solved. What if the first full attribute is true, and the second full atttribute is false and I want to increment the quantity attribute for the one that is false – Ricardo Martin Jul 09 '18 at 11:51
  • @RicardoMartin please check the answer – Ashh Jul 09 '18 at 11:56
  • The increment does not work as I expected because it increments both. – Ricardo Martin Jul 09 '18 at 12:10
  • 1
    @RicardoMartin check the updated one... It will increment the first one only... And please ask a new question If you have new doubt because you above query has been resolved thanks!!! – Ashh Jul 09 '18 at 12:25