[SERVER-14084] Adding compound index changes result set of query Created: 29/May/14  Updated: 10/Dec/14  Resolved: 29/May/14

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

Type: Bug Priority: Major - P3
Reporter: Michael Schoene Assignee: David Storch
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-13960 Queries containing $or may miss resul... Closed
Operating System: ALL
Steps To Reproduce:

Create collection:

db.getCollection("types").insert({
  "domainId": "type",
});
db.getCollection("types").insert({
  "domainId": "view",
});
db.getCollection("types").insert({
  "domainId": "vfsdata",
  "namespace": "eportal",
});
db.getCollection("types").insert({
  "domainId": "admin",
  "namespace": "eportal",
});

Test query:

db.types.find( { "$or" : [ { "namespace" : "eportal"} , { "namespace" :  null }] } )
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c13"), "domainId" : "type" }
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c14"), "domainId" : "view" }
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c15"), "domainId" : "vfsdata", "namespace" : "eportal" }
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c16"), "domainId" : "admin", "namespace" : "eportal" }

Add index:

db.types.ensureIndex( { namespace: 1, domainId: 1 }, { unique: 1 } )

Test query again:

db.types.find( { "$or" : [ { "namespace" : "eportal"} , { "namespace" :  null }] } )
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c13"), "domainId" : "type" }
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c14"), "domainId" : "view" }

Participants:

 Description   

I have a simple collection with these values:

{ "_id" : ObjectId("538602b59b6fe0b5efcf8c13"), "domainId" : "type" }
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c14"), "domainId" : "view" }
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c15"), "domainId" : "vfsdata", "namespace" : "eportal" }
{ "_id" : ObjectId("538602b59b6fe0b5efcf8c16"), "domainId" : "admin", "namespace" : "eportal" }

As expected the query

"$or" : [ { "namespace" : "eportal"} , { "namespace" :  null }]

found all documents in the collection.
If i add a unique compound index for the fields namespace and eportal, the same query found only the documents, where the field namespace exists.
In version 2.4 of MongoDB the second query found as expected all documents.



 Comments   
Comment by David Storch [ 29/May/14 ]

Hi mrs2207, thanks for reporting the bug! This is a known issue being tracked in SERVER-13960. A fix is currently in code review and is scheduled to be released in version 2.6.2.

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