[SERVER-12191] CanonicalQuery::canonicalize() rejects $elemMatch for DBRefs Created: 23/Dec/13  Updated: 11/Jul/16  Resolved: 05/Jan/14

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.5.4
Fix Version/s: 2.5.5

Type: Bug Priority: Major - P3
Reporter: Jeremy Mikola Assignee: Benety Goh
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-12263 Support $elemMatch with DBRefs that m... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

I encountered this on a 2.5.5 nightly, but I imagine the change was introduced somewhere in the 2.5.x branch.

> db.foo.find({ friends: { $elemMatch: { $ref: "people", $id: ObjectId('52b85ca3e84df160228b493b'), $db: "doctrine_odm_tests" } } })
error: {
	"$err" : "Can't canonicalize query: BadValue unknown top level operator: $ref",
	"code" : 17287
}

I don't believe there is a case where $elemMatch would have another operator as its immediate child, so perhaps this is just due to how the canonicalizer walks the query object. I noticed that it does not error out for the following queries:

> db.foo.find({ friend: { $ref: "people", $id: ObjectId('52b85ca3e84df160228b493b'), $db: "doctrine_odm_tests" } })
> db.foo.find({ friend: { $in: [{$ref: "people", $id: ObjectId('52b85ca3e84df160228b493b'), $db: "doctrine_odm_tests" }]}})



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

Author:

{u'username': u'benety', u'name': u'Benety Goh', u'email': u'benety@mongodb.com'}

Message: SERVER-12191 handle DBRef under $elemMatch
Branch: master
https://github.com/mongodb/mongo/commit/8bad5a8633eeca3bcbb975aabf268db9921f2c57

Comment by Scott Hernandez (Inactive) [ 23/Dec/13 ]

Yep, recompiled and tested and now it fails in the same way on master. Seems like this was introduced since https://github.com/mongodb/mongo/commit/a36903ef715cc962a254904e56b0ac1b7efd922f

Comment by Jeremy Mikola [ 23/Dec/13 ]

5ce04be64d3421479e69f8f53a097425c77cfe18.

> db.ta.save({_id:1, friends:[new DBRef("hi", 1), new DBRef("hey", 1)]})
Update WriteResult({
	"ok" : 1,
	"nDocsModified" : 0,
	"n" : 1,
	"upserted" : [
		{
			"index" : 0,
			"_id" : 1
		}
	]
})
> db.ta.find({ friends: { $elemMatch: { $ref: "hi", $id:1 }}})
error: {
	"$err" : "Can't canonicalize query: BadValue unknown top level operator: $ref",
	"code" : 17287
}
> db.runCommand('buildInfo')
{
	"version" : "2.5.5-pre-",
	"gitVersion" : "5ce04be64d3421479e69f8f53a097425c77cfe18",
	"OpenSSLVersion" : "",
	"sysInfo" : "Linux build3.nj1.10gen.cc 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 BOOST_LIB_VERSION=1_49",
	"loaderFlags" : "-static-libstdc++ -static-libgcc -fPIC -pthread -Wl,-z,now -rdynamic",
	"compilerFlags" : "-Wnon-virtual-dtor -Woverloaded-virtual -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -pipe -O3 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -fno-builtin-memcmp",
	"allocator" : "tcmalloc",
	"versionArray" : [
		2,
		5,
		5,
		-100
	],
	"javascriptEngine" : "V8",
	"bits" : 64,
	"debug" : false,
	"maxBsonObjectSize" : 16777216,
	"ok" : 1
}

Comment by Scott Hernandez (Inactive) [ 23/Dec/13 ]

What nightly was used? What git-hash?

I just tried this and it worked fine:

> db.ta.save({_id:1, friends:[new DBRef("hi", 1), new DBRef("hey", 1)]})
> db.ta.find({ friends: { $elemMatch: { $ref: "hi", $id:1 }}})
{ "_id" : 1, "friends" : [ DBRef("hi", 1), DBRef("hey", 1) ] }

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