[SERVER-2330] order of keys is matters in hashes, in find, similar object gives different results on different queries. Created: 05/Jan/11  Updated: 17/Mar/11  Resolved: 05/Jan/11

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

Type: Bug Priority: Major - P3
Reporter: Shimon Doodkin Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 10 server 64bit


Operating System: ALL
Participants:

 Description   

I cant expect programmers to remember the order in all places in the code especially in large code base.

db.eventstatus.insert(
{
"_id": ObjectId("4d2093d0aaed39b6d9716b1d"),
"t12_status": [

{ "status": "coming", "invite": ObjectId("4d208cdb50e82e8f65000002"), "value": 1 }

]
});

db.eventstatus.insert({
"_id": ObjectId("4d208dac50e82e8f6500000b"),
"t12_status": [

{ "invite": ObjectId("4d208cdb50e82e8f65000002"), "status": "coming", "value": 1 }

]
});

db.eventstatus.find({"t12_status":{"status":"coming","invite":ObjectId("4d208cdb50e82e8f65000002"),"value":1}});
db.eventstatus.find({"t12_status":{"invite":ObjectId("4d208cdb50e82e8f65000002"),"status":"coming","value":1}});

//expected: both queries will return the 2 documents. - wanted
//actual@v1.6.5: each query returns a different object, one object. - not wanted, unexpected



 Comments   
Comment by Eliot Horowitz (Inactive) [ 05/Jan/11 ]

That is what $elemMatch is for.
You should try that,

Comment by Shimon Doodkin [ 05/Jan/11 ]

then this test fails:

db.eventstatus.drop();
db.eventstatus.insert(
{
"_id": ObjectId("4d2093d0aaed39b6d9716b1d"),
"t12_status": [

{ "status": "coming", "invite": ObjectId("4d208cdb50e82e8f65000002"), "value": 2 }

,

{ "status": "ride", "invite": ObjectId("4d208cdb50e82e8f65000002"), "value": 1 }

]
});

db.eventstatus.find(

{"t12_status.status":"coming","t12_status.invite":ObjectId("4d208cdb50e82e8f65000002"),"t12_status.value":1}

);

{ "_id" : ObjectId("4d2093d0aaed39b6d9716b1d"), "t12_status" : [

{ "status" : "coming", "invite" : ObjectId("4d208cdb50e82e8f65000002"), "value" : 2 }

,

{ "status" : "ride", "invite" : ObjectId("4d208cdb50e82e8f65000002"), "value" : 1 }

] }

it returns incorrectly a document with no

{ "status" : "coming", "invite" : ObjectId("4d208cdb50e82e8f65000002"), "value" : 1 }

in the t12_status array

Comment by Eliot Horowitz (Inactive) [ 05/Jan/11 ]

BSON is ordered.
For most use cases this is correct.

If you don't care about order, you can query using

{ "a.b" : 1 , "a.c" : 1 }

style

Comment by Shimon Doodkin [ 05/Jan/11 ]

I could not find in my code where the order is switched.

Generated at Thu Feb 08 02:59:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.