[SERVER-16480] Query with $gt with Alphabate includes the results with Criteria Created: 09/Dec/14  Updated: 09/Dec/14  Resolved: 09/Dec/14

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

Type: Bug Priority: Major - P3
Reporter: Amit Kshirsagar Assignee: Ramon Fernandez Marina
Resolution: Done Votes: 0
Labels: query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

NA


Operating System: ALL
Participants:

 Description   

See below query,

db.first.find({name:{$gt:"A"}})
{ "_id" : ObjectId("5487373f1b8027ac7cddd0ab"), "name" : "Amit", "age" : 30, "gender" : "M" }
{ "_id" : ObjectId("548737b71b8027ac7cddd0ac"), "name" : "Amogh", "age" : 5, "gender" : "M" }
{ "_id" : ObjectId("548737f81b8027ac7cddd0ad"), "name" : "Poonam", "age" : 29, "gender" : "F" }

Because I am filtering the Collection based on Names greater than "A", where I should expect result shown as only below,

{ "_id" : ObjectId("548737f81b8027ac7cddd0ad"), "name" : "Poonam", "age" : 29, "gender" : "F" }

It displays all Three documents even with the name starting as "A".

If this is correct functionality, then what $gte:"A" should print out the documents?

Please let me know if you need any further details.

Thanks,
Amit



 Comments   
Comment by Amit Kshirsagar [ 09/Dec/14 ]

Thanks Ramon for the clarification.

Comment by Ramon Fernandez Marina [ 09/Dec/14 ]

amit.kshirsagar.13@gmail.com, this is the expected behavior. The comparison is done in lexicographical order, so "Amit" > "A". If you want to see results where name starts with letters B-Z, you can use:

db.first.find({name:{$gte:"B"}})

Here's a longer example that should help illustrate this behavior:

> db.foo.insert({name:"A"})
> db.foo.insert({name:"Alice"})
> db.foo.insert({name:"Bob"})
> db.foo.insert({name:"Charlie"})
> db.foo.find({name:{$gte:"A"}})
{ "_id" : ObjectId("548741279edccd131b648a10"), "name" : "A" }
{ "_id" : ObjectId("548741299edccd131b648a11"), "name" : "Alice" }
{ "_id" : ObjectId("5487412c9edccd131b648a12"), "name" : "Bob" }
{ "_id" : ObjectId("5487423e87c6b05030100d20"), "name" : "Charlie" }
> db.foo.find({name:{$gt:"A"}})
{ "_id" : ObjectId("548741299edccd131b648a11"), "name" : "Alice" }
{ "_id" : ObjectId("5487412c9edccd131b648a12"), "name" : "Bob" }
{ "_id" : ObjectId("5487423e87c6b05030100d20"), "name" : "Charlie" }
> db.foo.find({name:{$gte:"B"}})
{ "_id" : ObjectId("5487412c9edccd131b648a12"), "name" : "Bob" }
{ "_id" : ObjectId("5487423e87c6b05030100d20"), "name" : "Charlie" }

Regards,
Ramón.

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