1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
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 < arr.length; i++) {
total += distance(a, arr[i][f]);
}
return total;
}
queries = [{near: [7, 8], maxDistance: 3, search: {z: 3}}, ];
answers = queries.map(function() {
return {
totalDistance: 0,
results: []
};
});
n = 0;
for (x = 0; x < 20; x++) {
for (y = 0; y < 20; y++) {
t.insert({_id: n, loc: [x, y], z: [n % 10, (n + 5) % 10]});
for (i = 0; i < queries.length; i++) {
var d = distance(queries[i].near, [x, y]);
if (d > 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 < 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 );
}
|