[SERVER-16343] $pull using DBRef $id and $in operator Created: 27/Nov/14  Updated: 28/Nov/14  Resolved: 28/Nov/14

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.6.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Gabriel Brieva Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

I have this two documents:

{
foo : "test2",
rel : [

{ $ref : "bar", $id : ObjectId("54773af88328753e92484be8") }

,

{ $ref : "bar", $id : ObjectId("54480b5f18c7885d7651a140") }

]
}

{
foo : "test2",
rel : [

{ $ref : "bar", $id : ObjectId("54480b5f18c7885d7651a140") }

,

{ $ref : "bar", $id : ObjectId("54773af88328753e92484be8") }

]
}

When I use $pull to remove some of DBRef entries and using $in too, none of them are removed.

This is the query:

db.test.update({}, {$pull:
{ rel:
{ "$id":

{ $in: [ ObjectId("54773af88328753e92484be8"), ObjectId("5477954518c7885d7651a15a") ] }

}
}
})

Participants:

 Description   

When I use $pull to remove some DBRef using $id and $in operator, none of them are remove.

If the $id field have another name works.



 Comments   
Comment by Asya Kamsky [ 28/Nov/14 ]

That case should not work - I believe that may be a bug. I will check why this case works contrary to documentation.

Comment by Gabriel Brieva [ 28/Nov/14 ]

But in this case works:

Two documents:

{ "foo" : "test1", "rel" : [

{ "t" : "t", "id" : 2 }

,

{ "t" : "t", "id" : 3 }

] }

{ "foo" : "test2", "rel" : [

{ "t" : "t", "id" : 2 }

,

{ "t" : "t", "id" : 1 }

] }

The query:

db.test.update({}, {"$pull": { "rel": { "id":

{ "$in": [1, 3] }

} } },

{"multi": true}

)

That query remove

{"t" : "t", "id" : 1}

and

{"t" : "t", "id" : 3}

from documents. So, why don't works for DBRef case?

Comment by Asya Kamsky [ 28/Nov/14 ]

The result you're getting is correct.

Documentation for $pull specifies a number of behaviors:
http://docs.mongodb.org/manual/reference/operator/update/pull/#up._S_pull

Note it says:
If the specified <value> to remove is a document, $pull removes only the elements in the array that have the exact same fields and values.

You are asking to pull by trying to match document where "rel" has the value {"$id": ...} which is not the object that "rel" has.

What will work is:

db.test.update({},{"$pull":{"rel": { "$in": [{"$ref":"bar","$id": ObjectId("54773af88328753e92484be8")}, {"$ref":"bar","$id":ObjectId("5477954518c7885d7651a15a")} ] } }})

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