1

I am trying to update the month and the year fields in the incomes array but the command that I am using is substituting the object in the array for the updated object value(s). Note that the updated object has fewer key/value(s) than the original one.

{
"_id" : 12j3k123132,
"username" : "Alex",
"password" : "12345",
"email" : "a@ass",
"incomes" : [
    {
        "incomeId" : aisjdoaijfa,
        "month" : 10,
        "year" : 2017,
        "value" : 100.57,
        "description" : "car wash"
    }
],
"expenses" : [ ]

}

I am running the follow command, where updateInfo contains all the information that will be updated

incomeSearch.update({ _id: 12j3k123132), "incomes.incomeId": incomeId.incomeId }, {$set: {"incomes.$": updateInfo}}, (err, updateResult) => {

    if (err){
        res.status(500).send({error: `An error occurred. ${err}`});
    }
    res.status(200).send({msg: `Income updated`, result: updateResult});

});
  • Then actually update the matched `"month"` and `"year"` fields only. `{ "$set": { "incomes.$.month": 11, "incomes.$.year": 2016 } }`. As long as you specify the "full path" to each individual property, then these are the only things that actually update. – Neil Lunn Aug 01 '17 at 01:29
  • But I may want to update just month, or month, year and description.... How would I do that ? – Alexandre Amaral Aug 01 '17 at 01:32
  • That's what I'm showing you. Actually read it and notice the difference to what you are doing. – Neil Lunn Aug 01 '17 at 01:32
  • So you are saying, and mongodb too, that I can not update just some fields of an object inside an array without specifying the "full path" and keep the unchanged things there ? – Alexandre Amaral Aug 01 '17 at 01:40
  • How you are not seeing the obvious here escapes me. When you do `{ "$set": { "incomes.$": something } }` you are "replacing" the matched object with everything you provide. When you do `{ "$set": { "incomes.$.month": month, "incomes.$.year": year, "incomes.$.description": description } }` then the **only** things changed are the ones you actually asked to change. How would a database possibly know that `something` was meant to only update a couple of fields instead of overwriting the whole content? Just think for a minute. It's not hard to work out and understand. – Neil Lunn Aug 01 '17 at 01:44
  • I got it... However I am trying to understand why when I run this command incomeSearch.update(userId, {$set: {username: "Alexandre"}}, (err, updateResult).... it only updates the username, and only it, and does not overwrite the whole document. All the information that I want to be updated it is in the updateInfo variable... – Alexandre Amaral Aug 01 '17 at 01:52
  • 1
    @NeilLunn check this link: https://stackoverflow.com/questions/23127146/how-to-update-fields-of-a-nested-object-in-mongodb?rq=1 It is not possible to do it with mongo and they opened a ticket with this issue on MongoDB. – Alexandre Amaral Aug 01 '17 at 02:05
  • If you actually talking about "update multiple objects" as opposed to "multiple properties" then actually that question is **correctly** answered at [How to Update Multiple Array Elements in mongodb](https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb). But you "appear" to asking about "multiple properties" even though I'm really not sure you are even comprehending what you are asking at all. But thanks for pointing out that answer so I can close it as a duplicate of the other. – Neil Lunn Aug 01 '17 at 02:09

0 Answers0