0

I have a document like below:

{
    "_id" : ObjectId("52e51809454115b7c5d593a4"),
    "usage" : {
        "from" : "2013-12-02T00:00:00+01:00",
        "to" : "2014-02-02T00:00:00+01:00",
        "used" : 674999999,
        "total" : NumberLong(4000000000)
    },
        "refills" : [ 
        {
            "id" : "RXh0cmEgU3VyZnZvbHlt",
            "amount" : 1000000000,
            "fee" : "5520",
            "feeVat" : "6900",
            "validTo" : "2014-02-02T00:00:00+01:00"
        }
    ],
}

Where i would like to update the value "total" in "usage" and add to it the value "amount" in "refills" All other values should be the same.

How do i do that best?

morto
  • 73
  • 1
  • 8

1 Answers1

0

Assuming that refills is an object and not an array as in your example:

coll.find( ..., function( err, result) {
    ...
    coll.update( ..., { $set: { 'refills.amount': ... } }, function( err,result ) {
        ...
    });
});

So you first have to read the specific document to get the value of total and then update the field refills.amount. You can use $set or $inc depending on how you want to update.

EDIT:

If refills is an array it is not that different. See here.

Community
  • 1
  • 1
heinob
  • 19,127
  • 5
  • 41
  • 61