I've got a edge case with $geoNear that I'm having trouble figuring out.
I'm trying to use $geoNear to find other documents that are close by that belong to the same "group" in a collection.
When I search for results near document A, I see all 4 expected documents, including B. When I search for all documents near B, I find only 3 documents. The result for A is missing.
I tried to make reduced-test-case for the issue, whichi is below. The coordinates are the actual ones involved, but have been truncated for privacy.
I realized 'maxDistance' is in radians and is usually a fraction, but this case I wanted to set the number very high to make sure not exclude any results. I tried setting it as a fraction, which didn't help.
I don't think the "limit" is the issue either, as the limit is much higher than the number of results returned.
However, the reduced test case below doesn't reproduce the issue. If you run the below and inspect the results of `nearA` and `nearB` afterwards, you'll see that each can see the other.
However, if I use the same query structure on our actual database, I get the strange result that B is near A, but A is not found near B.
What am I missing that I could cause that result?
( I did also try dropping and re'adding the geospatial index on our actual data, but that didn't help either. )
Thanks,
Mark
( I'm using Mongo 2.4.10. )
var groupId = ObjectId('43dbd7f33554c0104a048d56');
var aId = ObjectId("43ea81d97782c2caf8475ec7");
var bId = ObjectId("43e933ae7782c2caf8475930");
var aCoords = [ -105.030, 39.912 ];
var bCoords = [ -105.116, 39.802 ];
db.tmp.drop();
db.tmp.insert([
{ name: 'A', _id : aId, groupId : [groupId], geometry : { "type": "Point", "coordinates": aCoords }},
{ name: 'B', _id : bId, groupId : [groupId], geometry : { "type": "Point", "coordinates": bCoords }}
]);
db.tmp.ensureIndex({'geometry':"2dsphere"});
var nearA = db.tmp.aggregate( [
{$geoNear:{
near : aCoords,
distanceField : "distance",
maxDistance : 1000,
spherical : true,
distanceMultiplier : 3959
}},
{$match:{
groupId : groupId,
_id : {$ne: aId }
}},
{$limit:30},
]);
var nearB = db.tmp.aggregate([
{$geoNear:{
near : bCoords,
distanceField : "distance",
maxDistance : 1000,
spherical : true,
distanceMultiplier : 3959
}},
{$match:{
groupId : groupId,
_id : {$ne: bId }
}},
{$limit:30},
]);