t = db.geo_haystack2 t.drop() function distance( a , b ){ var x = a[0] - b[0]; var y = a[1] - b[1]; return Math.sqrt( ( x * x ) + ( y * y ) ); } function distanceTotal( a , arr , f ){ var total = 0; for ( var i=0; i queries[i].maxDistance ) continue; if ( queries[i].search.z != n % 10 && queries[i].search.z != ( n + 5 ) % 10 ) continue; answers[i].results.push( { _id : n , loc : [ x , y ] } ) answers[i].totalDistance += d; } n++; } } t.ensureIndex( { loc : "geoHaystack" , z : 1 } , { bucketSize : .7 } ); for ( i=0; i