summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/lookup/lookup_subpipeline_geonear.js
blob: b199d03ae13a0bb2c5ace8e5f6062f709a950e1e (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
// Tests that $geoNear can be within a $lookup stage.
// TODO SERVER-29159: Enable test on passthroughs with sharded collections.
// $geoNear is not allowed in a facet even within a lookup.
// @tags: [
//   assumes_unsharded_collection,
//   do_not_wrap_aggregations_in_facets,
//   sbe_incompatible,
// ]
(function() {
"use strict";

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

coll.drop();
assert.commandWorked(coll.insert({_id: 4, x: 4}));

from.drop();

// Create geospatial index for field 'geo' on 'from'.
assert.commandWorked(from.createIndex({geo: "2dsphere"}));

// Insert one matching document in 'from'.
assert.commandWorked(from.insert({_id: 1, geo: [0, 0]}));

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

assert.eq(from.aggregate(geonearPipeline).itcount(), 1);

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

assert.eq(coll.aggregate(pipeline).toArray(),
          [{"_id": 4, "x": 4, "c": [{"_id": 1, "geo": [0, 0], "distance": 0}]}]);
}());