summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/lookup/lookup_absorb_match.js
blob: 1d85817970f7365fe20ad545d475b18bbedbed88 (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
/**
 * Tests that a $match with a geo expression still returns the correct results if it has been
 * absorbed by a $lookup.
 *
 * Accessed collections cannot be implicitly sharded because you cannot $lookup into a sharded
 * collection.
 * @tags: [assumes_unsharded_collection]
 */
(function() {
"use strict";

let testDB = db.getSiblingDB("lookup_absorb_match");
testDB.dropDatabase();

let locations = testDB.getCollection("locations");
assert.writeOK(locations.insert({_id: "doghouse", coordinates: [25.0, 60.0]}));
assert.writeOK(locations.insert({_id: "bullpen", coordinates: [-25.0, -60.0]}));

let animals = testDB.getCollection("animals");
assert.writeOK(animals.insert({_id: "dog", locationId: "doghouse"}));
assert.writeOK(animals.insert({_id: "bull", locationId: "bullpen"}));

// Test that a $match with $geoWithin works properly when performed directly on an absorbed
// lookup field.
let result = testDB.animals
                     .aggregate([
                         {
                           $lookup: {
                               from: "locations",
                               localField: "locationId",
                               foreignField: "_id",
                               as: "location"
                           }
                         },
                         {$unwind: "$location"},
                         {
                           $match: {
                               "location.coordinates": {
                                   $geoWithin: {
                                       $geometry: {
                                           type: "MultiPolygon",
                                           coordinates: [[[
                                               [20.0, 70.0],
                                               [30.0, 70.0],
                                               [30.0, 50.0],
                                               [20.0, 50.0],
                                               [20.0, 70.0]
                                           ]]]
                                       }
                                   }
                               }
                           }
                         }
                     ])
                     .toArray();
let expected =
    [{_id: "dog", locationId: "doghouse", location: {_id: "doghouse", coordinates: [25.0, 60.0]}}];
assert.eq(result, expected);

// Test that a $match with $geoIntersects works as expected when absorbed by a $lookup.
result = testDB.animals
                 .aggregate([
                     {
                       $lookup: {
                           from: "locations",
                           localField: "locationId",
                           foreignField: "_id",
                           as: "location"
                       }
                     },
                     {$unwind: "$location"},
                     {
                       $match: {
                           "location.coordinates": {
                               $geoIntersects: {
                                   $geometry: {
                                       type: "MultiPolygon",
                                       coordinates: [[[
                                           [-20.0, -70.0],
                                           [-30.0, -70.0],
                                           [-30.0, -50.0],
                                           [-20.0, -50.0],
                                           [-20.0, -70.0]
                                       ]]]
                                   }
                               }
                           }
                       }
                     }
                 ])
                 .toArray();
expected =
    [{_id: "bull", locationId: "bullpen", location: {_id: "bullpen", coordinates: [-25.0, -60.0]}}];
assert.eq(result, expected);
}());