Behavior of $near with a 2d index and a GeoJSON point has some surprises:
> db.test.createIndex({loc:'2d'}); > db.test.insert({_id: 1, loc:[0, 0]}); > // point [1, 0] is farther than 0 units from origin as expected > printjson(db.test.findOne({ ... loc:{ ... $near:[1, 0], ... $maxDistance: 0}})); null > // but GeoJSON point [1, 0] is 0 units from origin?? > printjson(db.test.findOne({ ... loc:{ ... $near:{ ... $geometry:{ ... type:'Point', ... coordinates:[1, 0]}}, ... $maxDistance: 0}})); { "_id" : 1, "loc" : [ 0, 0 ] } > // Wrong sort order: > db.test.insert({_id: 2, loc:[1, 0]}); > db.test.find({ ... loc:{ ... $near:{ ... $geometry:{ ... type:'Point', ... coordinates:[2, 0]}}}}); { "_id" : 1, "loc" : [ 0, 0 ] } { "_id" : 2, "loc" : [ 1, 0 ] } > // GeoJSON point [2, 0] is 1 unit from [1, 0], but 0 units from origin? > db.test.find({ ... loc:{ ... $near:{ ... $geometry:{ ... type:'Point', ... coordinates:[2, 0]}}, ... $maxDistance: 1}}); { "_id" : 1, "loc" : [ 0, 0 ] } { "_id" : 2, "loc" : [ 1, 0 ] } > > db.test.find({ ... loc:{ ... $near:{ ... $geometry:{ ... type:'Point', ... coordinates:[2, 0]}}, ... $maxDistance:.999}}); { "_id" : 1, "loc" : [ 0, 0 ] }
The documentation says "for GeoJSON points, use a 2dsphere index": should we uassert that GeoJSON points aren't used with 2d index, or fix the behavior?