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

$nearSphere query with 2d index, skip, and limit returns incomplete results

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.6.4
    • Affects Version/s: 2.6.3, 2.7.0
    • Component/s: Geo
    • None
    • ALL
    • Hide
      db.test.drop();
      db.test.ensureIndex({ "loc": "2d" });
      db.test.insert({
          "loc": { "long": 33, "lat": 38 },  // Far away
          "type": "restaurant"
      });
      
      db.test.insert({"loc": { "long": -122, "lat": 38 }});
      db.test.insert({"loc": { "long": -122, "lat": 37 }});
      
      jsTest.log('$nearSphere with skip(1) and limit(2):');
      var results = db.test.find({"loc": {"$nearSphere": [-122, 38.5]}}).skip(1).limit(2);
      print("count() is: ", results.count(true));
      print("but only one actual result:");
      print(tojson(results.toArray()));
      
      jsTest.log('Regular query with skip(1) and limit(2):');
      var results = db.test.find().skip(1).limit(2);
      print("count() is: ", results.count(true));
      print("and two results:");
      print(tojson(results.toArray()));
      
      Show
      db.test.drop(); db.test.ensureIndex({ "loc" : "2d" }); db.test.insert({ "loc" : { " long " : 33, "lat" : 38 }, // Far away "type" : "restaurant" }); db.test.insert({ "loc" : { " long " : -122, "lat" : 38 }}); db.test.insert({ "loc" : { " long " : -122, "lat" : 37 }}); jsTest.log( '$nearSphere with skip(1) and limit(2):' ); var results = db.test.find({ "loc" : { "$nearSphere" : [-122, 38.5]}}).skip(1).limit(2); print( "count() is: " , results.count( true )); print( "but only one actual result:" ); print(tojson(results.toArray())); jsTest.log( 'Regular query with skip(1) and limit(2):' ); var results = db.test.find().skip(1).limit(2); print( "count() is: " , results.count( true )); print( "and two results:" ); print(tojson(results.toArray()));

      Issue Status as of Aug 6, 2014

      ISSUE SUMMARY
      A $nearSphere query with a 2d index, a skip(m), and a limit(n) returns m results less than expected. If m and n are equal, zero results are returned.

      USER IMPACT
      Query results are incomplete or empty.

      WORKAROUNDS
      Replace the original value used in limit with the sum of the values used in limit + skip. For example, if using .skip(5).limit(4), specify .skip(5).limit(9) instead.

      AFFECTED VERSIONS
      MongoDB 2.6 production releases up to 2.6.3 are affected by this issue.

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

      RESOLUTION DETAILS
      When calculating how many documents to return, add the number of documents previously skipped.

      Original description

      A $nearSphere query with a 2d index, a skip, and a limit returns one less result than expected. If the limit is 1, then zero results are returned.

      Same thing happens if the limit is negative. Not observed with 2dsphere index.

      Discovered by Stefan Wójcik while adding geo tests to PyMongo.

            Assignee:
            siyuan.zhou@mongodb.com Siyuan Zhou
            Reporter:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Votes:
            2 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: