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

Positional Update with multiple elements

    XMLWordPrintable

    Details

    • Last comment by Customer:
      true
    • Actual Time:
      4

      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

          Issue Links

            Activity

              People

              Assignee:
              tim.slavin Tim Slavin
              Reporter:
              david.hows David Hows
              Participants:
              Last commenter:
              Eric Sedor Eric Sedor
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since reply:
                7 years, 21 weeks, 1 day ago
                Date of 1st Reply: