[SERVER-10725] incorrect results from $ne query using sparse index Created: 10/Sep/13  Updated: 11/Jul/16  Resolved: 07/Nov/13

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.4.6, 2.4.7, 2.5.3
Fix Version/s: 2.5.4

Type: Bug Priority: Major - P3
Reporter: Christophe Pizzuti Assignee: hari.khalsa@10gen.com
Resolution: Done Votes: 1
Labels: $ne, query, query_triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux on PC


Attachments: File server10725.js    
Operating System: ALL
Steps To Reproduce:

Commands from a mongo shell

db.user.drop()
db.user.ensureIndex(

{mail:1}

,

{unique:1, safe:1}

)
db.user.ensureIndex(

{"applications.last_ip":1}

,

{sparse:1, safe:1}

)
db.user.ensureIndex(

{"PushCtx.credentials":1}

,

{unique:1, sparse:1, dropDups:1, safe:1}

)
db.user.insert({ "_id" : ObjectId("522f16d7594e4df1158b48f1"), "administrative" :

{ "langage" : "french", "units" : "meter" }

, "applications" : [

{ "application" : ObjectId("522f16d7594e4df1158b48f2"), "access_token" : "522f16d7594e4df1158b48f1|uac2_1", "refresh_token" : "uref2_1" }

,

{ "application" : ObjectId("522f16d7594e4df1158b48f3"), "access_token" : "522f16d7594e4df1158b48f1|uac2_2", "refresh_token" : "uref2_2", "last_ip" : "1.1.1.1", "last_ip_date" : ISODate("2013-09-10T12:55:51.319Z") }

], "devices" : [ "unitmac2" ], "mail" : "unituser2@netatmo.com", "password" : "password" })
db.user.find({ _id: ObjectId('522f16d7594e4df1158b48f1'), "PushCtx.credentials": { $ne:

{ oauth_token: "token token token token token", oauth_token_secret: "token secret" }

} })

Participants:

 Description   

I have a collection having PushCtx.credentials and _id indexes.
According to base content a query of the kind
db.user.find({ _id: ObjectId('522f16d7594e4df1158b48f1'), "PushCtx.credentials": { $ne:

{ oauth_token: "token token token token token", oauth_token_secret: "token secret" }

} })
may fail (the entry exists and the query does not return it)

Adding hint({_id : 1}) makes the query work
db.user.find({ _id: ObjectId('522f16d7594e4df1158b48f1'), "PushCtx.credentials": { $ne:

{ oauth_token: "token token token token token", oauth_token_secret: "token secret" }

} }).hint({_id : 1})

{ "_id" : ObjectId("522f16d7594e4df1158b48f1"), (...) }

This is a problem for me, as I do have to perform an update on that query, and that hint does not work in such a case



 Comments   
Comment by a zhifan [ 11/Sep/13 ]

Yes, you are right. according to the current code of isSimpleIdQuery, do we aim at some queries which only have _id=? condition, not ( _id=? and xxx ... ). I think only if the query part is not ( _id=? or ... ), we can return true at isSimpleIdQuery(). but That would be more work to do.

Comment by Eliot Horowitz (Inactive) [ 11/Sep/13 ]

I think I see why that makes that query work, but its not doing the right thing.
That patch would ignore everything but the _id part of the query.
I think the real bug is deeper down.

Comment by a zhifan [ 11/Sep/13 ]

Very interested in mongodb.

seems the cause is function isSimpleIdQuery().

I can fix it with the following change:
while(i.more())

{ e = i.next(); if( strcmp("_id", e.fieldName()) == 0 && e.isSimpleType() ) return true; }

/*
BSONElement e = i.next();

if( i.more() )
return false;

if( strcmp("_id", e.fieldName()) != 0 )
return false;

if ( e.isSimpleType() ) // e.g. not something like { _id : { $gt : ...
*/

Comment by Eliot Horowitz (Inactive) [ 11/Sep/13 ]

Able to reproduce.
Works with non sparse indexes, so that is the workaround for now.

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