[SERVER-4395] $query operator not working in _addSpecial Created: 30/Nov/11  Updated: 12/Feb/13  Resolved: 07/May/12

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

Type: Bug Priority: Major - P3
Reporter: Uladzimir Mihura Assignee: Tad Marshall
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Operating System: ALL
Participants:

 Description   

In the documentation http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24query :

db.foo.find()._addSpecial( "$query" : {x : {$lt : 5}} )
// same as
db.foo.find({x : {$lt : 5}})

You can try this in shell:

> db.tmp.insert({"title":1, "field1":"value"})
> db.tmp.insert({"title":2, "field2":"value"})
> db.tmp.find({"title":2})
{ "_id" : ObjectId("4ed62f57e5505bab88fae758"), "title" : 2, "field2" : "value" }
> db.tmp.find()._addSpecial({"$query":{"title":2}})
{ "_id" : ObjectId("4ed62f56e5505bab88fae757"), "title" : 1, "field1" : "value" }
{ "_id" : ObjectId("4ed62f57e5505bab88fae758"), "title" : 2, "field2" : "value" }
> db.tmp.find({"$query":{"title":2}})
{ "_id" : ObjectId("4ed62f57e5505bab88fae758"), "title" : 2, "field2" : "value" }



 Comments   
Comment by Tad Marshall [ 07/May/12 ]

This is messed up in several ways:

1) The example given first, reportedly from the wiki ("db.foo.find()._addSpecial( "$query" : {x : {$lt : 5}} )"), does not work at all ... it gets an error from the shell ("SyntaxError: missing ) after argument list (shell):1"). There is no JavaScript syntax that lets you pass a function "$query":{}.
2) The example given in the code box is different but also wrong ... it passes an object ("{"$query":{"title":2}}") where the "name" argument to _addSpecial() should be and (implicitly) passes "undefined" as the value.

A "more correct" version of this usage would be 'db.tmp.find()._addSpecial("$query",

{"title":2}

)'. This also doesn't work but is at least syntactically correct.

The actual trouble in the "more correct" version is that the initial query (which is nothing in this case, the contents of the () in the find() part) is repackaged as "query" and then "$query" is appended. The original "query" takes precedence over the added "$query" and so the "$query" is not used.

A "fully correct" version of this is 'db.tmp.find()._addSpecial("query",

{"title":2}

)', where "query" is used instead of "$query". This actually works. It's not a very exciting feature, but it works.

Since this is now undocumented and is harmless and works if used correctly, I don't think this needs "fixing".

Comment by Uladzimir Mihura [ 14/Dec/11 ]

Ok Guys,

Thank you. So looks like this issue can be closed.

-vova

Comment by Eliot Horowitz (Inactive) [ 30/Nov/11 ]

This is not supposed to work - I updated the documentation.
We can make it throw an exception though.

Comment by Uladzimir Mihura [ 30/Nov/11 ]

The following code not working too:

>db.tmp.find()._addSpecial("$query",{"title":2})
{ "_id" : ObjectId("4ed62f56e5505bab88fae757"), "title" : 1, "field1" : "value" }
{ "_id" : ObjectId("4ed62f57e5505bab88fae758"), "title" : 2, "field2" : "value" }

-vova

Comment by Uladzimir Mihura [ 30/Nov/11 ]

I can see similar behavior for a Java driver.

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