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

Results of $near query on compound multi-key 2dsphere index not sorted by distance

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.6.2, 2.7.0
    • Affects Version/s: 2.6.0
    • Component/s: Geo, Querying
    • None
    • ALL

      Issue Status as of May 14, 2014

      ISSUE SUMMARY
      In the specific case where a compound 2dsphere index exists and is multi-key (at least one indexed value is an array), the wrong query plans can be generated for $near queries and lead to unexpected query results. For example, results returned by a $near query may no longer be sorted by distance.

      USER IMPACT
      Incorrect query plans can lead to unexpected behavior, specifically in the case of $near the user may get back incorrectly sorted documents from their query.

      WORKAROUNDS
      If possible, change the index to another compound field (in combination with the location) so it is no longer a multi-key index.

      AFFECTED VERSIONS
      Versions 2.6.0 and 2.6.1 are affected by this issue.

      FIX VERSION
      The fix is included in the 2.6.2 production release.

      RESOLUTION DETAILS
      The query plan enumerator now first assigns a mandatory predicate (e.g. $geonear), before assigning further predicates according to the usual rules of multi-key indexes.

      Original description

      I have been running a geospatial query using a compound index.

      After upgrading to MongoDB 2.6, the order of the result set has changed such that the results are no longer being sorted by distance for the following query:

      db.properties.find({ "address.uppercase": { $regex: "^14529" }, "address.location": { $near: { $geometry: { type: "Point", coordinates: [ -122.2103, 47.6154 ] }}}})
      

      The index on the properties collection is configured as:

      { "address.uppercase": 1, "address.location": "2dsphere" }
      

      In 2.4, the query returned all addresses where "address.uppercase" starts with "14529", ordered by the address distance from location [ -122.2103, 47.6154 ].

      After upgrading to 2.6, the same query now returns all addresses ordered by "address.uppercase", effectively making it impossible to perform a geospatial $near query on a compound index.

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            sprybytes Rick Huizinga
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: