summaryrefslogtreecommitdiff
path: root/jstests/core/geo_distinct.js
blob: bb2fd5bf85ba3644ec6795061af6fe4ca9c8cd94 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Tests distinct with geospatial field values.
// 1. Test distinct with geo values for 'key' (SERVER-2135)
// 2. Test distinct with geo predicates for 'query' (SERVER-13769)

var coll = db.geo_distinct;
var res;

//
// 1. Test distinct with geo values for 'key'.
//

coll.drop();
coll.insert( { loc: { type: 'Point', coordinates: [ 10, 20 ] } } );
coll.insert( { loc: { type: 'Point', coordinates: [ 10, 20 ] } } );
coll.insert( { loc: { type: 'Point', coordinates: [ 20, 30 ] } } );
coll.insert( { loc: { type: 'Point', coordinates: [ 20, 30 ] } } );
assert.eq( 4, coll.count() );

// Test distinct on GeoJSON points with/without a 2dsphere index.

res = coll.runCommand( 'distinct', { key: 'loc' } );
assert.commandWorked( res );
assert.eq( res.values.sort(), [ { type: 'Point', coordinates: [ 10, 20 ] },
                                { type: 'Point', coordinates: [ 20, 30 ] } ] );

assert.commandWorked( coll.ensureIndex( { loc: '2dsphere' } ) );

res = coll.runCommand( 'distinct', { key: 'loc' } );
assert.commandWorked( res );
assert.eq( res.values.sort(), [ { type: 'Point', coordinates: [ 10, 20 ] },
                                { type: 'Point', coordinates: [ 20, 30 ] } ] );

// Test distinct on legacy points with/without a 2d index.

// (Note that distinct on a 2d-indexed field doesn't produce a list of coordinate pairs, since
// distinct logically operates on unique values in an array.  Hence, the results are unintuitive and
// not semantically meaningful.)

coll.dropIndexes();

res = coll.runCommand( 'distinct', { key: 'loc.coordinates' } );
assert.commandWorked( res );
assert.eq( res.values.sort(), [ 10, 20, 30 ] );

assert.commandWorked( coll.ensureIndex( { 'loc.coordinates': '2d' } ) );

res = coll.runCommand( 'distinct', { key: 'loc.coordinates' } );
assert.commandWorked( res );
assert.eq( res.values.sort(), [ 10, 20, 30 ] );

//
// 2. Test distinct with geo predicates for 'query'.
//

coll.drop();
for (var i=0; i<50; ++i) {
    coll.insert( { zone: 1, loc: { type: 'Point', coordinates: [ -20, -20 ] } } );
    coll.insert( { zone: 2, loc: { type: 'Point', coordinates: [ -10, -10 ] } } );
    coll.insert( { zone: 3, loc: { type: 'Point', coordinates: [ 0, 0 ] } } );
    coll.insert( { zone: 4, loc: { type: 'Point', coordinates: [ 10, 10 ] } } );
    coll.insert( { zone: 5, loc: { type: 'Point', coordinates: [ 20, 20 ] } } );
}
var originGeoJSON = { type: 'Point', coordinates: [ 0, 0 ] };

// Test distinct with $nearSphere query predicate.

// A. Unindexed key, no geo index on query predicate.
res = coll.runCommand( 'distinct', { key: 'zone',
                                     query: { loc: { $nearSphere: { $geometry: originGeoJSON,
                                                                    $maxDistance: 1 } } } } );
assert.commandFailed( res );
// B. Unindexed key, with 2dsphere index on query predicate.
assert.commandWorked( coll.ensureIndex( { loc: '2dsphere' } ) );
res = coll.runCommand( 'distinct', { key: 'zone',
                                     query: { loc: { $nearSphere: { $geometry: originGeoJSON,
                                                                    $maxDistance: 1 } } } } );
assert.commandWorked( res );
assert.eq( res.values.sort(), [ 3 ] );
// C. Indexed key, with 2dsphere index on query predicate.
assert.commandWorked( coll.ensureIndex( { zone: 1 } ) );
res = coll.runCommand( 'distinct', { key: 'zone',
                                     query: { loc: { $nearSphere: { $geometry: originGeoJSON,
                                                                    $maxDistance: 1 } } } } );
assert.commandWorked( res );
assert.eq( res.values.sort(), [ 3 ] );

// Test distinct with $near query predicate (recall that $near returns the closest 100 points).

coll.dropIndexes();

// A. Unindexed key, no geo index on query predicate.
res = coll.runCommand( 'distinct', { key: 'zone',
                                     query: { 'loc.coordinates': { $near: [ 0, 0 ] } } } );
assert.commandFailed( res );
// B. Unindexed key, with 2d index on query predicate.
assert.commandWorked( coll.ensureIndex( { 'loc.coordinates': '2d' } ) );
res = coll.runCommand( 'distinct', { key: 'zone',
                                     query: { 'loc.coordinates': { $near: [ 0, 0 ] } } } );
assert.commandWorked( res );
assert.eq( res.values.sort(), [ 2, 3, 4 ] );
// C. Indexed key, with 2d index on query predicate.
assert.commandWorked( coll.ensureIndex( { zone: 1 } ) );
res = coll.runCommand( 'distinct', { key: 'zone',
                                     query: { 'loc.coordinates': { $near: [ 0, 0 ] } } } );
assert.commandWorked( res );
assert.eq( res.values.sort(), [ 2, 3, 4 ] );