[SERVER-18959] Cannot use comparaison operator to query ObjectID in embedded documents Created: 12/Jun/15  Updated: 30/Sep/15  Resolved: 13/Jun/15

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

Type: Bug Priority: Major - P3
Reporter: Sylvain Leroux [X] Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-20706 Comparison operators do not work embe... Closed
Operating System: ALL
Steps To Reproduce:

Given that collection:

> db.test.find().pretty()
{
	"_id" : ObjectId("557b43ed84ee2ba0375f4fca"),
	"user" : {
		"_id" : ObjectId("555b63c6b9aaf6d80aecccc3")
	}
}

$eq comparison on ObjectID works for the root document:

> db.test.find(
    {_id: { $eq: ObjectId("557b43ed84ee2ba0375f4fca") }}
).count(1)
1

But not for the embedded document:

> db.test.find(
    {user: { _id: { $eq: ObjectId("555b63c6b9aaf6d80aecccc3") }}}
).count()
0

Please note that using the dot notation to access the inner field, the query works:

> db.test.find(
    {"user._id": { $eq: ObjectId("555b63c6b9aaf6d80aecccc3") }}
).count()
1

Participants:

 Description   

You cannot use the relational operators $eq or $in to query ObjectID in an embedded document.

All is like is, for embedded documents, comparison was performed using ==, whereas for root document, it uses equals.



 Comments   
Comment by Sylvain Leroux [X] [ 13/Jun/15 ]

I fell so stupid.

When you perform an equality match on embedded document, using the query { field: <value> }, value is a document to match. Not a query document.

This is a non-issue, and should be closed.
Sorry for the noise.

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