[SERVER-12680] Cannot unset from all items in array Created: 11/Feb/14  Updated: 07/Mar/14  Resolved: 11/Feb/14

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.2.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: James Hartig Assignee: Unassigned
Resolution: Duplicate Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-1243 New operator to update all matching i... Closed
Operating System: ALL
Steps To Reproduce:

See example doc in description then run:
db.example.update({_id: ObjectId("52c5e5d2bd546795dee87ca4")}, {$unset: {"c.pi": 1}})

Participants:

 Description   

Running an "unset" on a key inside of array objects says it updated documents but it didn't.

Example doc:

{
        "_id" : ObjectId("52c5e5d2bd546795dee87ca4"),
        "c" : [
                {"pi" : NumberLong(2368999)},
                {"pi" : NumberLong(2368999)},
                {"pi" : NumberLong(2368999)}
        ]
}

Example query:

db.example.update({_id: ObjectId("52c5e5d2bd546795dee87ca4")}, {$unset: {"c.pi": 1}})

When I run that query using the PHP driver it returns:
"updatedExisting"= > true
"n" => 1

However nothing was actually unset. I know there's another bug filed (SERVER-1243) for a feature request to use a position operator on multiple items, but this should have affected ALL items in the collection, I'm not using $elemMatch or anything else. Is that not supported?

I said affects versions 2.2.3 because that's all we have running at Grooveshark but it might affect newer versions.



 Comments   
Comment by James Hartig [ 12/Feb/14 ]

I understand that it might not be the right way to unset, but it should throw an error then like $set does, "can't append to array using string field name", if it is really not the right way to do it.

Comment by Scott Hernandez (Inactive) [ 11/Feb/14 ]

Yes, that issue is the one to track and the syntax you are using does not do what you want, it will just unset an embedded field, not array elements.

Comment by James Hartig [ 11/Feb/14 ]

You can actually workaround this if you know all of the indexes by specifying each index separately:
db.example.update({_id: ObjectId("52c5e5d2bd546795dee87ca4")}, {$unset: {"c.0.pi": 1, "c.1.pi": 1, "c.2.pi": 1}})

But if your array is unbounded (as most are?) then you cannot do this.

Generated at Thu Feb 08 03:29:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.