summaryrefslogtreecommitdiff
path: root/jstests/aggregation/documents.js
blob: f9289a3baf0c46dc797ed1b2fb96ecc0535a8f53 (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
// This is the test for $documents stage in aggregation pipeline.
// The $documents stage follows these rules:
// * $documents must be in the beginning of the pipeline,
// * $documents content must evaluate to an array of objects.
// $documents is not meant to be used in a sharded environment. It would return
//  the same result set for each shard which is counter intuitive. The test is disabled
//  for mongos.
// @tags: [
//   do_not_wrap_aggregations_in_facets,
//   assumes_unsharded_collection,
//   assumes_read_preference_unchanged,
//   assumes_read_concern_unchanged,
//   assumes_against_mongod_not_mongos
// ]

(function() {
"use strict";
load('jstests/libs/fixture_helpers.js');  // for runCommandOnEachPrimary.

const writeConcernOptions = {
    writeConcern: {w: "majority"}
};

const currDB = db;
FixtureHelpers.runCommandOnEachPrimary(
    {db: db.getSiblingDB("admin"), cmdObj: {setParameter: 1, enableSearchMeta: true}});
const coll = currDB.documents;
coll.drop(writeConcernOptions);
coll.insert({a: 1}, writeConcernOptions);

// $documents given an array of objects.
const docs = coll.aggregate([{$documents: [{a1: 1}, {a1: 2}]}], writeConcernOptions).toArray();

assert.eq(2, docs.length);
assert.eq(docs[0], {a1: 1});
assert.eq(docs[1], {a1: 2});

// $documents evaluates to an array of objects.
const docs1 =
    coll.aggregate([{$documents: {$map: {input: {$range: [0, 100]}, in : {x: "$$this"}}}}],
                   writeConcernOptions)
        .toArray();

assert.eq(100, docs1.length);
for (let i = 0; i < 100; i++) {
    assert.eq(docs1[i], {x: i});
}

// $documents evaluates to an array of objects.
const docsUnionWith =
    coll.aggregate(
            [
                {$documents: [{a: 13}]},
                {
                    $unionWith: {
                        pipeline:
                            [{$documents: {$map: {input: {$range: [0, 10]}, in : {x: "$$this"}}}}]
                    }
                }
            ],
            writeConcernOptions)
        .toArray();

assert.eq(11, docsUnionWith.length);
assert.eq(docsUnionWith[0], {a: 13});
for (let i = 1; i < 11; i++) {
    assert.eq(docsUnionWith[i], {x: i - 1});
}

// $documents with const objects inside $unionWith (no "coll").
const res = coll.aggregate([{$unionWith: {pipeline: [{$documents: [{xx: 1}, {xx: 2}]}]}}],
                           writeConcernOptions)
                .toArray();
assert.eq(3, res.length);
assert.eq(res[0]["a"], 1);
assert.eq(res[1], {xx: 1});
assert.eq(res[2], {xx: 2});

function assertFails(pipeline, code) {
    assert.commandFailedWithCode(currDB.runCommand({
        aggregate: coll.getName(),
        pipeline: pipeline,
        writeConcern: writeConcernOptions.writeConcern,
        cursor: {}
    }),
                                 code);
}

// Must fail due to misplaced $document.
assertFails([{$project: {a: [{xx: 1}, {xx: 2}]}}, {$documents: [{a: 1}]}], 40602);
// $unionWith must fail due to no $document
assertFails([{$unionWith: {pipeline: [{$project: {a: [{xx: 1}, {xx: 2}]}}]}}], 9);

// Must fail due to $documents producing array of non-objects.
assertFails([{$documents: [1, 2, 3]}], 40228);

// Must fail due $documents producing non-array.
assertFails([{$documents: {a: 1}}], 5858203);

// Must fail due $documents producing array of non-objects.
assertFails([{$documents: {a: [1, 2, 3]}}], 5858203);
})();