Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-9484

2dsphere Index may not return all documents for $near and geoNear

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4.4, 2.5.0
    • Affects Version/s: 2.4.3
    • Component/s: Geo, Index Maintenance
    • None
    • Environment:
      Windows 7 x64
    • ALL
    • Hide

      Import data file (attached) into MongoDB using mongoImport to database "sparcTest" and collection "TirCSV". Ensure 2dsphere index on "geometry" field.
      Perform .count() on a global find. Note it returns 3063 elements.
      Perform $geoNear query above and note it misses some data. Record number of elements returned.
      Perform $within query above and note it returns all data.
      Reindex TirCSV collection.
      Perform $geoNear query and record number of elements returned – may be different.
      Perform $geoWithin query and note it still returns all data.

      Show
      Import data file (attached) into MongoDB using mongoImport to database "sparcTest" and collection "TirCSV". Ensure 2dsphere index on "geometry" field. Perform .count() on a global find. Note it returns 3063 elements. Perform $geoNear query above and note it misses some data. Record number of elements returned. Perform $within query above and note it returns all data. Reindex TirCSV collection. Perform $geoNear query and record number of elements returned – may be different. Perform $geoWithin query and note it still returns all data.

      When performing a $near or geoNear query on GeoJSON data indexed with a 2dsphere index, the query appears to miss data. This may be an issue with the 2dsphere index, as reindexing changes the number of data elements returned for a geoNear query, but not for a bounding-box $geoWithin query. In this case, all of the data is within the polygon bounded by longitude -90 to -80, and latitude 35 to 45. The only secondary index on the collection is

      {"geometry" : "2dsphere"}

      .

      > db.TirCSV.count({ "geometry" : { "$near" : { "$geometry" :

      { "type" : "Point", "coordinates" : [ -85.389 , 40.46]}

      }} , "$or" : [

      { "data_type" : "tir"}

      ]})
      3017
      > db.TirCSV.find({geometry : {$geoWithin : {$geometry : {type : "Polygon", coordinates: [[[-90,45],[-90,35],[-80,35],[-80,45],[-90,45]]]}}}}).count()
      3063
      > db.TirCSV.count(

      {"data_type" : "tir"}

      )
      3063
      > db.TirCSV.reIndex()
      {
      "nIndexesWas" : 2,
      "msg" : "indexes dropped for collection",
      "nIndexes" : 2,
      "indexes" : [
      {
      "key" :

      { "_id" : 1 }

      ,
      "ns" : "sparcTest.TirCSV",
      "name" : "id"
      },
      {
      "key" :

      { "geometry" : "2dsphere" }

      ,
      "ns" : "sparcTest.TirCSV",
      "name" : "geometry_2dsphere"
      }
      ],
      "ok" : 1
      }
      > db.TirCSV.count({ "geometry" : { "$near" : { "$geometry" :

      { "type" : "Point", "coordinates" : [ -85.389 , 40.46]}

      }} , "$or" : [

      { "data_type" : "tir"}

      ]})
      3016
      > db.TirCSV.find({geometry : {$geoWithin : {$geometry : {type : "Polygon", coordinates: [[[-90,45],[-90,35],[-80,35],[-80,45],[-90,45]]]}}}}).count()
      3063

        1. geoIntersects.kml
          986 kB
          Jim Dagg
        2. near.kml
          974 kB
          Jim Dagg
        3. out.json
          2.56 MB
          Jim Dagg
        4. QueryDiff.js
          2 kB
          Jim Dagg

            Assignee:
            hari.khalsa@10gen.com hari.khalsa@10gen.com
            Reporter:
            jdagg Jim Dagg
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: