0

I have a document like :

{
"_id": "1000",
"answer_count": 0,
"answer_order": 0,
"fields": [{
    "name": "client_name",
    "id": "com28",
    "title": "Client",
    "required": true,
    "instruct": "",
    "dateType": "text"
}, {
    "name": "id_radio",
    "id": "com26",
    "title": "hobby",
    "required": false,
    "instruct": "",
    "value": [{
        "name": "Please select",
        "selected": true,
        "lid": "-1",
        "defaultTip": true
    }, {
        "name": "option1",
        "selected": false,
        "lid": "0"
    }, {
        "name": "option2",
        "selected": false,
        "lid": "1"
    }, {
        "name": "option3",
        "selected": false,
        "lid": "2"
    }]
}]
}

I try:

db.survey_stat.update({
     "fields.id" : "com26", 
     "fields.value.name":"option1"
 },
 {
     "$inc":{
         "fields.0.value.$.selected_count":1
     }
 })

But it doesn't update any field. How can I incr the specific field inside a array ?

Philipp
  • 67,764
  • 9
  • 118
  • 153
sharewind
  • 2,299
  • 4
  • 16
  • 12
  • possible duplicate of [Update embedded object inside array inside array in MongoDB](http://stackoverflow.com/questions/10465849/update-embedded-object-inside-array-inside-array-in-mongodb) – dgiugg Aug 14 '14 at 07:41

1 Answers1

0

The array-entry with fields.id: "com26" which you want to change is fields.1 not fields.0. This means you are trying to change the wrong entry of the fields array.

Unfortunately the $-placeholder can only be used once in a field-path. When you have two nested arrays, it won't work. There is an open bugtracker ticket about this issue. The only workaround is to retrieve and replace the whole array-entry in which the nested field occurs.

Philipp
  • 67,764
  • 9
  • 118
  • 153