summaryrefslogtreecommitdiff
path: root/jstests/libs/geo_near_random.js
diff options
context:
space:
mode:
authorKyle Suarez <kyle.suarez@mongodb.com>2018-06-18 23:34:49 -0400
committerKyle Suarez <kyle.suarez@mongodb.com>2018-06-18 23:34:49 -0400
commit7bc7864fc042b69d36a88c6839c5dd5b4eb20693 (patch)
treee103e752e5d708aa22dca3ae99ef269d79a5d917 /jstests/libs/geo_near_random.js
parent7c89f48c4f1f1e3ede2931ab602fa118281530a2 (diff)
downloadmongo-7bc7864fc042b69d36a88c6839c5dd5b4eb20693.tar.gz
SERVER-35043, SERVER-22949: move geoNear implementation into aggregation
This commit removes the geoNear command and moves its implementation into the aggregation framework. Users should use the aggregate command with a $geoNear stage. The implementation rewrite additionally removes the limit in the $geoNear aggregation stage. To limit the number of results, use a $limit stage.
Diffstat (limited to 'jstests/libs/geo_near_random.js')
-rw-r--r--jstests/libs/geo_near_random.js62
1 files changed, 27 insertions, 35 deletions
diff --git a/jstests/libs/geo_near_random.js b/jstests/libs/geo_near_random.js
index 207ca1302d9..d5de7aa70a7 100644
--- a/jstests/libs/geo_near_random.js
+++ b/jstests/libs/geo_near_random.js
@@ -43,17 +43,17 @@ GeoNearRandomTest.prototype.insertPts = function(nPts, indexBounds, scale) {
this.t.ensureIndex({loc: '2d'}, indexBounds);
};
-GeoNearRandomTest.prototype.assertIsPrefix = function(short, long) {
+GeoNearRandomTest.prototype.assertIsPrefix = function(short, long, errmsg) {
for (var i = 0; i < short.length; i++) {
- var xS = short[i].obj ? short[i].obj.loc[0] : short[i].loc[0];
- var yS = short[i].obj ? short[i].obj.loc[1] : short[i].loc[1];
- var dS = short[i].obj ? short[i].dis : 1;
+ var xS = short[i] ? short[i].loc[0] : short[i].loc[0];
+ var yS = short[i] ? short[i].loc[1] : short[i].loc[1];
+ var dS = short[i] ? short[i].dis : 1;
- var xL = long[i].obj ? long[i].obj.loc[0] : long[i].loc[0];
- var yL = long[i].obj ? long[i].obj.loc[1] : long[i].loc[1];
- var dL = long[i].obj ? long[i].dis : 1;
+ var xL = long[i] ? long[i].loc[0] : long[i].loc[0];
+ var yL = long[i] ? long[i].loc[1] : long[i].loc[1];
+ var dL = long[i] ? long[i].dis : 1;
- assert.eq([xS, yS, dS], [xL, yL, dL]);
+ assert.eq([xS, yS, dS], [xL, yL, dL], errmsg);
}
};
@@ -66,37 +66,29 @@ GeoNearRandomTest.prototype.testPt = function(pt, opts) {
print("testing point: " + tojson(pt) + " opts: " + tojson(opts));
- var cmd = {geoNear: this.t.getName(), near: pt, num: 1, spherical: opts.sphere};
+ let query = {loc: {}};
+ query.loc[opts.sphere ? '$nearSphere' : '$near'] = pt;
+ const proj = {dis: {$meta: "geoNearDistance"}};
+ const runQuery = (limit) => this.t.find(query, proj).limit(opts.nToTest).toArray();
- var last = this.db.runCommand(cmd).results;
+ let last = runQuery(1);
for (var i = 2; i <= opts.nToTest; i++) {
- // print(i); // uncomment to watch status
- cmd.num = i;
- var ret = this.db.runCommand(cmd).results;
-
- try {
- this.assertIsPrefix(last, ret);
- } catch (e) {
- print("*** failed while compairing " + (i - 1) + " and " + i);
- printjson(cmd);
- throw e; // rethrow
- }
-
- // Make sure distances are in increasing order
- assert.gte(ret[ret.length - 1].dis, last[last.length - 1].dis);
+ let ret = runQuery(i);
+ this.assertIsPrefix(last, ret, `Unexpected result when comparing ${i-1} and ${i}`);
+ // Make sure distances are in increasing order.
+ assert.gte(ret[ret.length - 1].dis, last[last.length - 1].dis);
last = ret;
}
- last = last.map(function(x) {
- return x.obj;
- });
-
- var query = {loc: {}};
- query.loc[opts.sphere ? '$nearSphere' : '$near'] = pt;
- var near = this.t.find(query).limit(opts.nToTest).toArray();
-
- // Test that a query using $near/$nearSphere with a limit of 'nToTest' returns the same points
- // (in order) as the geoNear command with num=nToTest.
- assert.eq(last, near);
+ // Test that a query using $near or $nearSphere returns the same points in order as the $geoNear
+ // aggregation stage.
+ const queryResults = runQuery(opts.nToTest);
+ const aggResults = this.t
+ .aggregate([
+ {$geoNear: {near: pt, distanceField: "dis", spherical: opts.sphere}},
+ {$limit: opts.nToTest}
+ ])
+ .toArray();
+ assert.eq(queryResults, aggResults);
};