[DOCS-3276] Positional Update with multiple elements Created: 26/Apr/14  Updated: 03/Jun/19  Resolved: 05/Jun/14

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: v1.3.6

Type: Task Priority: Major - P3
Reporter: David Hows Assignee: Tim Slavin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

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


Issue Links:
Related
related to DOCS-12773 Explicitly refer to "Single Field Mat... Closed
Participants:
Days since reply: 9 years, 36 weeks, 6 days ago

 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"}})



 Comments   
Comment by Githook User [ 05/Jun/14 ]

Author:

{u'username': u'TimSlavinMongoDB', u'name': u'Tim Slavin (MongoDB)', u'email': u'tim.slavin@10gen.com'}

Message: DOCS-3276: added elemMatch section, updated headings

Signed-off-by: kay <kay.kim@10gen.com>
Branch: master
https://github.com/mongodb/docs/commit/1baa7afbd49c9bee2212c398ce05049673f336d6

Generated at Thu Feb 08 07:45:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.