[SERVER-8593] Incorrect results when projecting a field with a hashed index Created: 15/Feb/13  Updated: 06/Dec/22  Resolved: 07/Jan/16

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: 2.2.3, 2.4.0-rc0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Sridhar Nanjundeswaran Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

2.2.3 and 2.4.0-rc0 on OS X


Assigned Teams:
Query
Operating System: ALL
Steps To Reproduce:

var coll = db.getCollection("hashed_projection")
coll.drop()
 
for(var i=0; i<5; i++) {
    coll.insert({foo:i})
}
coll.ensureIndex({foo: "hashed"});
 
coll.find({foo : {$lt:3}}, {foo:1, _id:0}).hint({foo: "hashed"});

Participants:

 Description   

From the documentation you cannot use hashed indexes for range queries. Hence expect the query below to either error out or return the correct result.
This query does the right thing if the hashed index is not specified on the hint. Also additionally if you create a regular index on the foo field the query correctly picks the regular index and returns the correct result.



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

It looks like this has been fixed, probably as part of the query engine rewrite first released in version 2.6.0:

> var coll = db.getCollection("hashed_projection")
> coll.drop()
> for(var i=0; i<5; i++) { coll.insert({foo:i}); }
> coll.ensureIndex({foo: "hashed"});
> coll.find({foo : {$lt:3}}, {foo:1, _id:0}).hint({foo: "hashed"});
{ "foo" : 2 }
{ "foo" : 0 }
{ "foo" : 1 }

Closing as Gone Away.

Comment by Scott Hernandez (Inactive) [ 03/Mar/13 ]

The range query doesn't make a lot of sense for a hashed index, but this does to get values from the index instead of docs:

coll.find({}, {foo:1,_id:0}).hint({foo:"hashed"})

This returns the hash value, not the field value.

{ "foo" : NumberLong("-7726858113162202750") }

Comment by Sridhar Nanjundeswaran [ 15/Feb/13 ]

One more minor test. If the query is changed to an equality it does the right thing too.

> coll.find({foo : 3}, {foo:1, _id:0}).hint({foo:"hashed"})
{ "foo" : 3 }

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