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

Positional Update with multiple elements

    XMLWordPrintableJSON

Details

    Description

      http://docs.mongodb.org/manual/reference/operator/update/positional/

      This page only mentions updates which involve single point array element matches. If users wish to update with positional operators that match on multiple elements they need to use the $elemMatch operator. We should update this page to reflect that.

      Example:
      Consider the following document.

      db.test.insert({     "_id" : ObjectId("535aee4647280195f1a078ca"), "array" : [          {            "id" : "11b0b4f5-43ca-43f9-b568-f7d8f6bdc803-0", "name" : "Jacob", "status" : "working"}, {             "id" : "11b0b4f5-43ca-43f9-b568-f7d8f6bdc803-1",             "name" : "David", "status" : "working" } ] })

      I wish to set the status of David to sleeping.

      If i run the following, then Jacob (the first possible match) is updated.

      db.test.update({ "array.name" : "David" , "array.status" : "working"}, {$set: {"array.$.status" : "sleeping"}})

      To update the David subdoc I must use elemMatch:

      db.test.update({ "array" : {$elemMatch: {"name" : "David" , "status" : "working"}}}, {$set: {"array.$.status" : "sleeping"}})

      Attachments

        Activity

          People

            tim.slavin Tim Slavin
            david.hows David Hows
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              9 years, 36 weeks, 6 days ago