0

I have set a 2dSphereIndex on a field named location, performing the same $near query with the smallest (1 meter) variation on $maxDistance leads to total different results:

First query:

db.collection.find(
{
   "location":{
      "$near":{
         "$geometry":{
            "type":"Point",
            "coordinates":[
               12.0,
               0.2
            ]
         },
         "$maxDistance":3724073
      }
   }
})
.limit(100).explain("executionStats");

Stats first query:

"executionTimeMillis" : 0,
"totalKeysExamined" : 4,
"totalDocsExamined" : 0,

Second query:

db.collection.find(
{
   "location":{
      "$near":{
         "$geometry":{
            "type":"Point",
            "coordinates":[
               12.0,
               0.2
            ]
         },
         "$maxDistance":3724074
      }
   }
})
.limit(100).explain("executionStats");

Stats second query:

"executionTimeMillis" : 411, 
"totalKeysExamined" : 56473,
"totalDocsExamined" : 56471,
...

The points are scattered all over the countries starting from a certain latitude so I would not expect an impact of 50,000+ elements on a meter variation.

Additional question, why does $near not apply the limit condition directly on the index during scan, but afterwards?

Additional details: "dbVersion":"4.4.4","isEnterprise":false

Cheers

Sergio
  • 89
  • 1
  • 6
  • How many documents the first query returned? – nimrod serok May 11 '22 at 08:47
  • First element is at around 3,930,100 meters. Coordinates [12.60166, 35.50015]. Query stats: "nReturned" : 1, "executionTimeMillis" : 448, "totalKeysExamined" : 56473, "totalDocsExamined" : 56471. Tried to regenerate index, no luck – Sergio May 12 '22 at 10:01
  • The query returned only one document? please add here 3 more documents that it should return. – nimrod serok May 12 '22 at 10:20
  • These are the first for southwise [12.60166,35.50015],[12.6056,35.5007],[12.61057,35.5011],[12.61354,35.50142]. The total number of elements in the database are around 100,000. – Sergio May 12 '22 at 14:59

0 Answers0