[SERVER-9812] User query is interpreted incorrectly if the first field is "query" Created: 29/May/13  Updated: 01/Sep/15  Resolved: 12/Aug/15

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

Type: Bug Priority: Minor - P4
Reporter: Seraj Ahmad Assignee: David Storch
Resolution: Duplicate Votes: 1
Labels: query_triage

Attachments: File server9812.js    
Issue Links:
Duplicate
duplicates SERVER-15176 New commands for find and getMore Closed
is duplicated by SERVER-11864 $in operator fails if document field ... Closed
is duplicated by SERVER-14952 count and find inconsistent when usin... Closed
is duplicated by SERVER-20241 "Query" as a property name is allowed... Closed
is duplicated by SERVER-17171 the query for collection with "query"... Closed
Related
related to SERVER-8025 shell supports "query" works as a met... Closed
related to DOCS-1560 Document behavior of using the field ... Closed
is related to SERVER-6767 Interpret $query as special so you ca... Closed
Backwards Compatibility: Major Change
Operating System: ALL
Participants:

 Comments   
Comment by Seraj Ahmad [ 29/May/13 ]

Here is how to reproduce it in version 2.4.3

db.sample.insert({'a': 1, 'q': { 'b': 2, 'c: 3 }})
db.sample.insert({'a': 1, 'dquery': { 'b': 2, 'c: 3 }})
db.sample.insert({'a': 1, 'query': { 'b': 2, 'c: 3 }})
db.sample.find({'q': {'b': 2, 'c': 3}}) - SUCCEEDS
db.sample.find({'dquery': {'b': 2, 'c': 3}}) - SUCCEEDS
db.sample.find({'query': {'b': 2, 'c': 3}}) - FAILS

Comment by Scott Hernandez [ 29/May/13 ]

This is unfortunately expected behavior. In order to do your "query" you will need to wrap your query like this:

db.sample.find({ '$query': {'query': {'b': 2, 'c': 3}}}) // or without "$" in older versions.

This behavior exists for historic, and internal, reasons and is not currently something which is easy to fix because of backwards compatibility, which it would break. We are working on removing this special behavior but we are not there yet.

Comment by Seraj Ahmad [ 30/May/13 ]

I was using 1.7.x version before and it was working. I was wondering how come it is not backward compatible.

Comment by Seraj Ahmad [ 30/May/13 ]

Also, the field 'query' need not be first field in the query.
This will also not work:

db.sample.find({'a': 1, 'query': { 'b': 2, 'c': 3 }})

Comment by Seraj Ahmad [ 30/May/13 ]

Is this behaviour documented somewhere? I can't find any documentation related to this. Are there any other such restrictions on queries?

Comment by Dan Pasette [ 03/Jun/13 ]

It's not currently documented but I did add a ticket for it here: DOCS-1560.

Comment by David Storch [ 28/May/15 ]

We expect this to be resolved with the introduction of the find and getMore commands (see SERVER-15176).

Comment by David Storch [ 12/Aug/15 ]

Per my earlier comment, this will be resolved with SERVER-15176. Once this ticket is resolved, the query db.foo.find({query: {b: 1}}) will correctly match documents that have a literal query field with a nested document {b: 1}:

> db.foo.insert({query: {b: 1}})
> db.foo.insert({b: 1})
> db.foo.find({query: {b: 1}})
{ "_id" : ObjectId("55cbbfc9cc5679501a43d4c5"), "query" : { "b" : 1 } }

Closing as a duplicate.

Generated at Thu Dec 13 09:14:12 UTC 2018 using Jira 7.12.1#712002-sha1:609a50578ba6bc73dbf8b05dddd7c04a04b6807c.