[SERVER-14564] explain() doesn't work with $min / $max Created: 15/Jul/14  Updated: 10/Dec/14  Resolved: 15/Jul/14

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

Type: Bug Priority: Major - P3
Reporter: Siyuan Zhou 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
Related
Operating System: ALL
Participants:

 Description   

The following query runs successfully on 2.6.3, but explains doesn't allow $min / $max.

> db.bar.ensureIndex({ a: 1})
{
	"createdCollectionAutomatically" : true,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> db.bar.insert({a: 100})
WriteResult({ "nInserted" : 1 })
> db.bar.find({ $min: { a: 1}, $query: {} })
{ "_id" : ObjectId("53c553de010c89b3b2c76040"), "a" : 100 }
> db.bar.find({ $max: { a: 1}, $query: {} }).explain()
2014-07-15T12:09:53.686-0400 error: {
	"$err" : "Can't canonicalize query: BadValue unknown top level operator: $max",
	"code" : 17287
} at src/mongo/shell/query.js:131



 Comments   
Comment by J Rassi [ 15/Jul/14 ]

I am curious if we have any plan to make explain() work with $query / $min / $max?

Yep: SERVER-6767. Resolving as dup.

Comment by Siyuan Zhou [ 15/Jul/14 ]

rassi@10gen.com - it works perfect with {$explain: true}, thanks!

I am curious if we have any plan to make explain() work with $query / $min / $max? Since I feel like it's a reasonable expectation from users to explain() any query. If it's not possible technically, the documentation might need to update for $explain and explain().

Comment by J Rassi [ 15/Jul/14 ]

Could you try with {$explain: true} instead of .explain()? See also <http://docs.mongodb.org/manual/reference/operator/meta/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.

Therefore, the following two operations are equivalent:

db.collection.find( { $query: { age : 25 }, $explain: true } )
db.collection.find( { age : 25 } ).explain()

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