summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/lookup_unwind_getmore.js
blob: 3ba7dbf4007ec04943150c9509362b70568cbaf7 (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
/**
 * Tests that the server correctly handles when the OperationContext used by the $lookup stage
 * changes as it unwinds the results.
 *
 * This test was designed to reproduce SERVER-22537.
 * @tags: [
 *   requires_spawning_own_processes,
 * ]
 */
(function() {
    'use strict';

    const options = {setParameter: 'internalDocumentSourceCursorBatchSizeBytes=1'};
    const conn = MongoRunner.runMongod(options);
    assert.neq(null, conn, 'mongod was unable to start up with options: ' + tojson(options));

    const testDB = conn.getDB('test');

    /**
     * Executes an aggregrate with 'options.pipeline' and confirms that 'options.numResults' were
     * returned.
     */
    function runTest(options) {
        // The batchSize must be smaller than the number of documents returned by the $lookup. This
        // ensures that the mongo shell will issue a getMore when unwinding the $lookup results for
        // the same document in the 'source' collection, under a different OperationContext.
        const batchSize = 2;

        testDB.source.drop();
        assert.writeOK(testDB.source.insert({x: 1}));

        testDB.dest.drop();
        for (let i = 0; i < 5; ++i) {
            assert.writeOK(testDB.dest.insert({x: 1}));
        }

        const res = assert.commandWorked(testDB.runCommand({
            aggregate: 'source',
            pipeline: options.pipeline,
            cursor: {
                batchSize: batchSize,
            },
        }));

        const cursor = new DBCommandCursor(testDB, res, batchSize);
        assert.eq(options.numResults, cursor.itcount());
    }

    runTest({
        pipeline: [
            {
              $lookup: {
                  from: 'dest',
                  localField: 'x',
                  foreignField: 'x',
                  as: 'matches',
              }
            },
            {
              $unwind: {
                  path: '$matches',
              },
            },
        ],
        numResults: 5
    });

    runTest({
        pipeline: [
            {
              $lookup: {
                  from: 'dest',
                  let : {x1: "$x"},
                  pipeline: [
                      {$match: {$expr: {$eq: ["$$x1", "$x"]}}},
                      {
                        $lookup: {
                            from: "dest",
                            as: "matches2",
                            let : {x2: "$x"},
                            pipeline: [{$match: {$expr: {$eq: ["$$x2", "$x"]}}}]
                        }
                      },
                      {
                        $unwind: {
                            path: '$matches2',
                        },
                      },
                  ],
                  as: 'matches1',
              }
            },
            {
              $unwind: {
                  path: '$matches1',
              },
            },
        ],
        numResults: 25
    });

    MongoRunner.stopMongod(conn);
})();