Hide
                
                    
In the query below, we request documents where b exists. As expected, we get 0 results.
When an index is added on (location, b), all documents are returned when $exists: true is set for b, which is incorrect.
                 
             
            
                Show
                
                                              
 > db.foo.drop()
true
> db.foo.insert({a: 1, "location" : [ -117.15, 32.88 ]})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({a: 2, "location" : [ -117.15, 32.88 ]})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({a: 3, "location" : [ -117.15, 32.88 ]})
WriteResult({ "nInserted" : 1 })
> db.foo.ensureIndex({location: "2d"})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
 
  
 In the query below, we request documents where b exists. As expected, we get 0 results. 
  
 > db.foo.find({location: { $nearSphere: [ -117.2684194, 33.1029176 ], $maxDistance: 0.02526017985248055 }, b: { $exists: true}})
# no results (expected)
 
  
 When an index is added on (location, b), all documents are returned when $exists: true is set for b, which is incorrect. 
  
 > db.foo.ensureIndex({location: "2d", b: 1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
}
> db.foo.find({location: { $nearSphere: [ -117.2684194, 33.1029176 ], $maxDistance: 0.02526017985248055 }, b: { $exists: true}}).hint("location_2d_b_1")
{ "_id" : ObjectId("562524f65a47d987934ec69b"), "a" : 1, "location" : [ -117.15, 32.88 ] }
{ "_id" : ObjectId("562524fb5a47d987934ec69c"), "a" : 2, "location" : [ -117.15, 32.88 ] }
{ "_id" : ObjectId("562525005a47d987934ec69d"), "a" : 3, "location" : [ -117.15, 32.88 ] }