summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/geonear/requires_geo_index.js
blob: e2c3a1d9706735e099a3bed47a38efa5c43ecd81 (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
// Test that $geoNear requires a geospatial index.
// TODO: Reenable test on passthroughs with sharded collections as part of SERVER-38995.
// @tags: [assumes_unsharded_collection]
(function() {
    "use strict";

    load("jstests/aggregation/extras/utils.js");  // For "assertErrorCode".

    const coll = db.coll;
    const from = db.from;

    coll.drop();
    from.drop();

    const geonearPipeline = [
        {$geoNear: {near: [0, 0], distanceField: "distance", spherical: true}},
    ];

    const geonearWithinLookupPipeline = [
        {
          $lookup: {
              pipeline: geonearPipeline,
              from: from.getName(),
              as: "c",
          }
        },
    ];

    assert.commandWorked(coll.insert({_id: 5, x: 5}));
    assert.commandWorked(from.insert({_id: 1, geo: [0, 0]}));

    // Fail without index.
    assertErrorCode(from, geonearPipeline, ErrorCodes.IndexNotFound);
    assertErrorCode(coll, geonearWithinLookupPipeline, ErrorCodes.IndexNotFound);

    assert.commandWorked(from.createIndex({geo: "2dsphere"}));

    // Run successfully when you have the geospatial index.
    assert.eq(from.aggregate(geonearPipeline).itcount(), 1);
    assert.eq(coll.aggregate(geonearWithinLookupPipeline).itcount(), 1);
}());