summaryrefslogtreecommitdiff
path: root/jstests/core/multikey_geonear.js
blob: ad24afb0c988286d31a5e7ffb0d59b997c405f31 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Test that we correct return results for compound 2d and 2dsphere indices in
// both the multikey and non-multikey cases.
// @tags: [
//   sbe_incompatible,
// ]

var t = db.jstests_multikey_geonear;
t.drop();

// Check that the result set from the cursor is the document with _id: 2,
// followed by _id: 1, and _id: 0.
function checkResults(cursor) {
    for (var i = 2; i >= 0; i--) {
        assert.eq(i, cursor.next()["_id"]);
    }
    assert(!cursor.hasNext());
}

t.ensureIndex({a: 1, b: "2dsphere"});

t.insert({_id: 0, a: 0, b: {type: "Point", coordinates: [0, 0]}});
t.insert({_id: 1, a: 0, b: {type: "Point", coordinates: [1, 1]}});
t.insert({_id: 2, a: 0, b: {type: "Point", coordinates: [2, 2]}});

// Ensure that the results are returned sorted by increasing distance.
var cursor = t.find({a: {$gte: 0}, b: {$near: {$geometry: {type: "Point", coordinates: [2, 2]}}}});
checkResults(cursor);

// The results should be the same if we make the "a" field multikey.
t.insert({_id: 3, a: ["multi", "key"], b: {type: "Point", coordinates: [-1, -1]}});
cursor = t.find({a: {$gte: 0}, b: {$near: {$geometry: {type: "Point", coordinates: [2, 2]}}}});
checkResults(cursor);

// Repeat these tests for a 2d index.
t.drop();
t.ensureIndex({a: "2d", b: 1});
t.insert({_id: 0, a: [0, 0], b: 0});
t.insert({_id: 1, a: [1, 1], b: 1});
t.insert({_id: 2, a: [2, 2], b: 2});

cursor = t.find({a: {$near: [2, 2]}, b: {$gte: 0}});
checkResults(cursor);

t.insert({_id: 3, a: [-1, -1], b: ["multi", "key"]});
cursor = t.find({a: {$near: [2, 2]}, b: {$gte: 0}});
checkResults(cursor);

// The fields in the compound 2dsphere index share a prefix.
t.drop();
t.ensureIndex({"a.b": 1, "a.c": "2dsphere"});
t.insert({_id: 0, a: [{b: 0}, {c: {type: "Point", coordinates: [0, 0]}}]});
t.insert({_id: 1, a: [{b: 1}, {c: {type: "Point", coordinates: [1, 1]}}]});
t.insert({_id: 2, a: [{b: 2}, {c: {type: "Point", coordinates: [2, 2]}}]});

cursor =
    t.find({"a.b": {$gte: 0}, "a.c": {$near: {$geometry: {type: "Point", coordinates: [2, 2]}}}});
checkResults(cursor);

// Double check that we're not intersecting bounds. Doing so should cause us to
// miss the result here.
t.insert({_id: 3, a: [{b: 10}, {b: -1}, {c: {type: "Point", coordinates: [0, 0]}}]});
cursor = t.find(
    {"a.b": {$lt: 0, $gt: 9}, "a.c": {$near: {$geometry: {type: "Point", coordinates: [0, 0]}}}});
assert.eq(3, cursor.next()["_id"]);
assert(!cursor.hasNext());

// The fields in the compound 2d index share a prefix.
t.drop();
t.ensureIndex({"a.b": "2d", "a.c": 1});
t.insert({_id: 0, a: [{b: [0, 0]}, {c: 0}]});
t.insert({_id: 1, a: [{b: [1, 1]}, {c: 1}]});
t.insert({_id: 2, a: [{b: [2, 2]}, {c: 2}]});

cursor = t.find({"a.b": {$near: [2, 2]}, "a.c": {$gte: 0}});
checkResults(cursor);