0

What is the query to update below data with mongoose. So 3 fields are going to be updated. Top Parent Points, Categories Points and Tag Points.

{
"_id": "561fba5e7fac41a4055fad45",
"fullName": "Test",
"points": 45,
"level": 1,
"categories": [
  {
    "name": "Computer Science",
    "points": 15,
    "level": 1,
    "_id": "561fba5e7fac41a4055fad46",
    "tags": [
      {
        "name": "C#",
        "points": 10,
        "level": 1,
        "_id": "561fba5e7fac41a4055fad47"
      },
      {
        "name": "Java",
        "points": 5,
        "level": 1,
        "_id": "561fba5e7fac41a4055ert12"
      }
    ]
  },
  {
    "name": "History",
    "points": 30,
    "level": 2,
    "_id": "562407d4e3edf2113f61ac37",
    "tags": [
      {
        "name": "WW2",
        "points": 30,
        "level": 2,
        "_id": "56240797e3edf2113f61ac36"
      }
    ]
  }
]
}

to this one. When user gets a point from a specific tag, it will effect all parents. Let's say, user gets 10 points from C# then i have to update mongodb to this.

{
"_id": "561fba5e7fac41a4055fad45",
"fullName": "Test",
**"points": 55,**
"level": 1,
"categories": [
  {
    "name": "Computer Science",
    **"points": 25,**
    "level": 1,
    "_id": "561fba5e7fac41a4055fad46",
    "tags": [
      {
        "name": "c#",
        **"points": 20,**
        "level": 1,
        "_id": "561fba5e7fac41a4055fad47"
      },
      {
        "name": "Java",
        "points": 5,
        "level": 1,
        "_id": "561fba5e7fac41a4055ert12"
      }
    ]
  },
  {
    "name": "History",
    "points": 30,
    "level": 2,
    "_id": "562407d4e3edf2113f61ac37",
    "tags": [
      {
        "name": "WW2",
        "points": 30,
        "level": 2,
        "_id": "56240797e3edf2113f61ac36"
      }
    ]
  }
]
}
Yagiz Ozturk
  • 5,408
  • 7
  • 29
  • 44
  • Possible duplicate of [Updating nested arrays in mongoDB via mongo shell](http://stackoverflow.com/questions/18573117/updating-nested-arrays-in-mongodb-via-mongo-shell) – chridam Oct 20 '15 at 08:08
  • Possible duplicate of [Update all elements in an array in mongodb](http://stackoverflow.com/questions/33186596/update-all-elements-in-an-array-in-mongodb) – styvane Oct 20 '15 at 08:27
  • These are not duplicates as I am trying to increase all the top parent fields. @user3100115 – Yagiz Ozturk Oct 20 '15 at 10:48
  • Not duplicate @chridam but thanks anyway – Yagiz Ozturk Oct 20 '15 at 10:49

1 Answers1

1

you should use $elemMatch for querying your object

db.tests.update({_id: yourTestId, categories: {$elemMatch: {_id: categoryId}}}, {$set: {$inc: {"categories.$.points":  10, points: 10}}})

So you querying only needed array element and update it values with $ reference

vmkcom
  • 1,630
  • 11
  • 17
  • 1
    {$inc} doesnt work out @vmkcom This works db.users.update({ categories : { $elemMatch: { name: "Sport" } } }, {$inc: {"categories.$.points": 6666, points : 7777}}) – Yagiz Ozturk Oct 20 '15 at 14:00
  • ofc, @YagizOzturk, forgot operator order. – vmkcom Oct 20 '15 at 14:30
  • thanks for the answer first, helped me really. But what about the 2nd child which is C#. Is there a way to update its point also within the same query. In your example it doesnt get incremented @vmkcom – Yagiz Ozturk Oct 20 '15 at 15:06
  • that's only for "one" level of nesting. You definitely needs to restructure your data. look at this for example http://stackoverflow.com/questions/4121666/updating-nested-arrays-in-mongodb – vmkcom Oct 21 '15 at 07:41