0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00
mongodb/jstests/geo_haystack1.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2010-06-27 17:54:49 +02:00
2010-07-23 16:57:50 +02:00
t = db.geo_haystack1
2010-06-27 17:54:49 +02:00
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<arr.length; i++ ){
total += distance( a , arr[i][f] );
}
return total;
}
2010-06-27 17:54:49 +02:00
queries = [
{ near : [ 7 , 8 ] , maxDistance : 3 , search : { z : 3 } } ,
]
answers = queries.map( function(){ return { totalDistance : 0 , results : [] }; } )
2010-06-27 17:54:49 +02:00
n = 0;
for ( x=0; x<20; x++ ){
for ( y=0; y<20; y++ ){
t.insert( { _id : n , loc : [ x , y ] , z : n % 5 } );
for ( i=0; i<queries.length; i++ ){
var d = distance( queries[i].near , [ x , y ] )
if ( d > queries[i].maxDistance )
2010-06-27 17:54:49 +02:00
continue;
if ( queries[i].search.z != n % 5 )
continue;
answers[i].results.push( { _id : n , loc : [ x , y ]} )
answers[i].totalDistance += d;
2010-06-27 17:54:49 +02:00
}
n++;
}
}
2010-07-23 16:57:50 +02:00
t.ensureIndex( { loc : "geoHaystack" , z : 1 } , { bucketSize : .7 } );
2010-06-27 17:54:49 +02:00
for ( i=0; i<queries.length; i++ ){
print( "---------" );
printjson( queries[i] );
res = t.runCommand( "geoSearch" , queries[i] )
print( "\t" + tojson( res.stats ) );
print( "\tshould have: " + answers[i].results.length + "\t actually got: " + res.stats.n );
assert.eq( answers[i].results.length , res.stats.n, "num:"+ i + " number matches" )
assert.eq( answers[i].totalDistance , distanceTotal( queries[i].near , res.results , "loc" ), "num:"+ i + " totalDistance" )
//printjson( res );
//printjson( answers[i].length );
2010-06-27 17:54:49 +02:00
}