Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-2197

Comment on: "manual/reference/operator/update/positional.txt"

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 01112017-cleanup
    • None
    • None

    Description

      Hi. Let use next doc for example:

      db.test.save({
          _id: 1,
          categories: [
              {parent_id: 1001, _id: 1, name: 'one'},
              {parent_id: 1001, _id: 2, name: 'two'},
              {parent_id: 1001, _id: 3, name: 'three'},
          ]
      })

      When i'm doing:

      db.test.update({_id: 1, 'categories._id': 2, 'categories.parent_id': 1001}, {$set: {'categories.$.name': 'two - UPDATED'}})

      expecting to see second sub-document's name change. However, this query will update first sub-doc:

      db.test.find({_id: 1}).pretty()
      {
      	"_id" : 1,
      	"categories" : [
      		{
      			"_id" : 1,
      			"name" : "two - UPDATED",
      			"parent_id" : 1001
      		},
      		{
      			"parent_id" : 1001,
      			"_id" : 2,
      			"name" : "two"
      		},
      		{
      			"parent_id" : 1001,
      			"_id" : 3,
      			"name" : "three"
      		}
      	]
      }

      To get expected results I'll have to move 'categories._id' parameter in query to the end:

      db.test.update({_id: 1, 'categories.parent_id': 1001, 'categories._id': 2}, {$set: {'categories.$.name': 'two - UPDATED'}})

      Attachments

        Activity

          People

            Unassigned Unassigned
            auto auto
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              10 years, 15 weeks ago