summaryrefslogtreecommitdiff
path: root/jstests/sharding/geo_near_sort.js
diff options
context:
space:
mode:
authorTess Avitabile <tess.avitabile@mongodb.com>2017-12-19 17:43:50 -0500
committerTess Avitabile <tess.avitabile@mongodb.com>2017-12-28 11:01:26 -0500
commitf98cb60d80f281d3065b0282ed6f25b5f419ae1b (patch)
treeef3834ff588eaab6ff8382fa9f4bc74ebbff7d57 /jstests/sharding/geo_near_sort.js
parente64b7e331649fd1d6beb83d981857ffd7ad6e539 (diff)
downloadmongo-f98cb60d80f281d3065b0282ed6f25b5f419ae1b.tar.gz
SERVER-1981 Support near and nearSphere predicates on sharded collections
Diffstat (limited to 'jstests/sharding/geo_near_sort.js')
-rw-r--r--jstests/sharding/geo_near_sort.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/jstests/sharding/geo_near_sort.js b/jstests/sharding/geo_near_sort.js
new file mode 100644
index 00000000000..713a8007722
--- /dev/null
+++ b/jstests/sharding/geo_near_sort.js
@@ -0,0 +1,90 @@
+// Tests that the sort specification is obeyed when the query contains $near/$nearSphere.
+(function() {
+ "use strict";
+
+ const st = new ShardingTest({shards: 2});
+ const db = st.getDB("test");
+ const coll = db.geo_near_sort;
+ const caseInsensitive = {locale: "en_US", strength: 2};
+
+ assert.commandWorked(st.s0.adminCommand({enableSharding: db.getName()}));
+ st.ensurePrimaryShard(db.getName(), "shard0000");
+ assert.commandWorked(st.s0.adminCommand({shardCollection: coll.getFullName(), key: {_id: 1}}));
+
+ // Split the data into 2 chunks and move the chunk with _id > 0 to shard 1.
+ assert.commandWorked(st.s0.adminCommand({split: coll.getFullName(), middle: {_id: 0}}));
+ assert.commandWorked(
+ st.s0.adminCommand({movechunk: coll.getFullName(), find: {_id: 1}, to: "shard0001"}));
+
+ // Insert some documents. The sort order by distance from the origin is [-2, 1, -1, 2] (under 2d
+ // or 2dsphere geometry). The sort order by {a: 1} under the case-insensitive collation is [2,
+ // -1, 1, -2]. The sort order by {b: 1} is [2. -1, 1, -2].
+ const docMinus2 = {_id: -2, geo: [0, 0], a: "BB", b: 3};
+ const docMinus1 = {_id: -1, geo: [0, 2], a: "aB", b: 1};
+ const doc1 = {_id: 1, geo: [0, 1], a: "Ba", b: 2};
+ const doc2 = {_id: 2, geo: [0, 3], a: "aa", b: 0};
+ assert.writeOK(coll.insert(docMinus2));
+ assert.writeOK(coll.insert(docMinus1));
+ assert.writeOK(coll.insert(doc1));
+ assert.writeOK(coll.insert(doc2));
+
+ function testSortOrders(query, indexSpec) {
+ assert.commandWorked(coll.createIndex(indexSpec));
+
+ // Test a $near/$nearSphere query without a specified sort. The results should be sorted by
+ // distance from the origin.
+ let res = coll.find(query).toArray();
+ assert.eq(res.length, 4, tojson(res));
+ assert.eq(res[0], docMinus2, tojson(res));
+ assert.eq(res[1], doc1, tojson(res));
+ assert.eq(res[2], docMinus1, tojson(res));
+ assert.eq(res[3], doc2, tojson(res));
+
+ // Test with a limit.
+ res = coll.find(query).limit(2).toArray();
+ assert.eq(res.length, 2, tojson(res));
+ assert.eq(res[0], docMinus2, tojson(res));
+ assert.eq(res[1], doc1, tojson(res));
+
+ if (db.getMongo().useReadCommands()) {
+ // Test a $near/$nearSphere query sorted by {a: 1} with the case-insensitive collation.
+ res = coll.find(query).collation(caseInsensitive).sort({a: 1}).toArray();
+ assert.eq(res.length, 4, tojson(res));
+ assert.eq(res[0], doc2, tojson(res));
+ assert.eq(res[1], docMinus1, tojson(res));
+ assert.eq(res[2], doc1, tojson(res));
+ assert.eq(res[3], docMinus2, tojson(res));
+
+ // Test with a limit.
+ res = coll.find(query).collation(caseInsensitive).sort({a: 1}).limit(2).toArray();
+ assert.eq(res.length, 2, tojson(res));
+ assert.eq(res[0], doc2, tojson(res));
+ assert.eq(res[1], docMinus1, tojson(res));
+ }
+
+ // Test a $near/$nearSphere query sorted by {b: 1}.
+ res = coll.find(query).sort({b: 1}).toArray();
+ assert.eq(res.length, 4, tojson(res));
+ assert.eq(res[0], doc2, tojson(res));
+ assert.eq(res[1], docMinus1, tojson(res));
+ assert.eq(res[2], doc1, tojson(res));
+ assert.eq(res[3], docMinus2, tojson(res));
+
+ // Test with a limit.
+ res = coll.find(query).sort({b: 1}).limit(2).toArray();
+ assert.eq(res.length, 2, tojson(res));
+ assert.eq(res[0], doc2, tojson(res));
+ assert.eq(res[1], docMinus1, tojson(res));
+
+ assert.commandWorked(coll.dropIndex(indexSpec));
+ }
+
+ testSortOrders({geo: {$near: [0, 0]}}, {geo: "2d"});
+ testSortOrders({geo: {$nearSphere: [0, 0]}}, {geo: "2d"});
+ testSortOrders({geo: {$near: {$geometry: {type: "Point", coordinates: [0, 0]}}}},
+ {geo: "2dsphere"});
+ testSortOrders({geo: {$nearSphere: {$geometry: {type: "Point", coordinates: [0, 0]}}}},
+ {geo: "2dsphere"});
+
+ st.stop();
+})();