[SERVER-22308] query in collection which has different type filed, just return one same type res Created: 26/Jan/16  Updated: 26/Jan/16  Resolved: 26/Jan/16

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

Type: Bug Priority: Major - P3
Reporter: YANG Chenghu Assignee: David Storch
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to DOCS-7044 Document type bracketing Closed
Operating System: ALL
Participants:

 Description   

Google Group URL:https://groups.google.com/forum/#!topic/mongodb-dev/bjFGvzB8OAE

rs0:PRIMARY> db.a.find().sort({_id:1})
{ "_id" : 1 }
{ "_id" : 2 }
{ "_id" : 3 }
{ "_id" : "1" }
{ "_id" : "10" }
{ "_id" : "100" }
{ "_id" : "2" }
{ "_id" : "20" }
{ "_id" : "3" }
{ "_id" : { "nam" : "1234" } }
{ "_id" : { "name" : "123" } }
{ "_id" : { "name" : "1234" } }
{ "_id" : { "name2" : "1234" } }
 
 
rs0:PRIMARY> db.a.find({_id:{$lt:MaxKey, $gt:"2"}})
{ "_id" : "20" }
{ "_id" : "3" }



 Comments   
Comment by David Storch [ 26/Jan/16 ]

Hi ych.tiger@gmail.com,

Thanks for reporting this ticket. It looks to me like this is the expected behavior of the system, so I am closing as "Works as Designed". Please read on for a more detailed description of why this is the expected behavior.

The MongoDB query language semantics include a concept called "type bracketing". This means that when you issue a range predicate with a particular canonical type, the predicate will only match values up to the minimum or maximum value for that type. (Canonical type differs from BSON type; numbers can have BSON type NumberLong, NumberInt, or NumberDouble, but all three have the canonical type of "number".) This means that the predicate {_id: {$gt: "2"}} corresponds to the range ("2", {}). The upper end of this range is an empty nested object ("{}"), which is the first non-string value that compares higher than any string. Since the upper bound is exclusive, the range effectively means "all strings greater than '2'". Documents in which the '_id' field is not a string will not match this query. This is why your query only returns documents with _id "20" and "3".

I performed a quick search of our documentation and did not find any good documentation on type bracketing. Therefore, I filed a ticket on your behalf requesting this documentation: see DOCS-7044.

Please let me know if you have any further questions.

Best,
Dave

Generated at Thu Feb 08 04:00:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.