[SERVER-10352] doing explain on $query on the shell returns error 'bad query' Created: 26/Jul/13  Updated: 10/Dec/14  Resolved: 26/Jul/13

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

Type: Bug Priority: Major - P3
Reporter: Gianfranco Palumbo Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-6767 Interpret $query as special so you ca... Closed
Operating System: ALL
Participants:

 Description   

> db.test.find({$query: {random: -14 } }).explain()
Fri Jul 26 16:51:50.251 error: {
	"$err" : "bad query: BadValue unknown top level operator: $query",
	"code" : 16810
} at src/mongo/shell/query.js:131
 
> db.test.getIndexes()
[
	{
		"v" : 1,
		"key" : {
			"_id" : 1
		},
		"ns" : "test.test",
		"name" : "_id_"
	}
]

Fri Jul 26 16:51:07.277 [initandlisten] MongoDB starting : pid=81884 port=27017 dbpath=/data/db/ 64-bit host=Gianfranco-10gen.local
Fri Jul 26 16:51:07.277 [initandlisten]
Fri Jul 26 16:51:07.277 [initandlisten] ** NOTE: This is a development version (2.5.1) of MongoDB.
Fri Jul 26 16:51:07.277 [initandlisten] **       Not recommended for production.
Fri Jul 26 16:51:07.277 [initandlisten]
Fri Jul 26 16:51:07.277 [initandlisten] db version v2.5.1
Fri Jul 26 16:51:07.278 [initandlisten] git version: dd226d98a53286a5af892f0b81a5252dd62f7444
Fri Jul 26 16:51:07.278 [initandlisten] build info: Darwin bs-osx-106-x86-64-2.10gen.cc 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49

https://github.com/mongodb/mongo/blob/r2.5.1/src/mongo/shell/query.js#L131

The only change I can see in that portion of code is:

SERVER-10134 Report errors during GetMore processing
This is necessary for SERVER-5932 (aggregation returning cursors)

https://github.com/mongodb/mongo/commit/0f43cfcbe063c1f4615ce362e751155218224bec



 Comments   
Comment by J Rassi [ 26/Jul/13 ]

It doesn't actually "work" on 2.4.5 – it runs the explain on the wrong query. See Scott's first comment in SERVER-6767 for an explanation.

To see in action:

> db.foo.find({_id:0}).explain()
{
	"cursor" : "BtreeCursor _id_",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 1,
	"nscanned" : 1,
	"nscannedObjectsAllPlans" : 1,
	"nscannedAllPlans" : 1,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		"start" : {
			"_id" : 0
		},
		"end" : {
			"_id" : 0
		}
	},
	"server" : "Rassi-MacBook-Pro.local:1338"
}
> db.foo.find({$query:{_id:0}}).explain()
{
	"cursor" : "BasicCursor",
	"isMultiKey" : false,
	"n" : 0,
	"nscannedObjects" : 1,
	"nscanned" : 1,
	"nscannedObjectsAllPlans" : 1,
	"nscannedAllPlans" : 1,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
 
	},
	"server" : "Rassi-MacBook-Pro.local:1338"
}

Comment by Gianfranco Palumbo [ 26/Jul/13 ]

Oh ok. It works on 2.4.5 so that was what tipped me off.

Closing.

Comment by J Rassi [ 26/Jul/13 ]

You can't use $query with .explain(). From http://docs.mongodb.org/manual/reference/operator/query/:

Do not mix query forms. If you use the $query format, do not append cursor methods to the find(). To modify the query use the meta-query operators, such as $explain.

See dup SERVER-6767.

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