summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/command_let_variables.js
blob: aa04bedbf629d3db4a042ede80d65f940efb8f4d (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Tests that commands like find, aggregate and update accepts a 'let' parameter which defines
// variables for use in expressions within the command.
// TODO SERVER-46707: move this back to core after let params work in sharded aggreggate.
// @tags: [assumes_against_mongod_not_mongos, requires_fcv46]

(function() {
"use strict";

const coll = db.update_let_variables;
coll.drop();

assert.commandWorked(coll.insert([
    {
        Species: "Blackbird (Turdus merula)",
        population_trends: [
            {term: {start: 1970, end: 2014}, pct_change: -16, annual: -0.38, trend: "no change"},
            {term: {start: 2009, end: 2014}, pct_change: -2, annual: -0.36, trend: "no change"}
        ]
    },
    {
        Species: "Bullfinch (Pyrrhula pyrrhula)",
        population_trends: [
            {term: {start: 1970, end: 2014}, pct_change: -39, annual: -1.13, trend: "no change"},
            {term: {start: 2009, end: 2014}, pct_change: 12, annual: 2.38, trend: "weak increase"}
        ]
    },
    {
        Species: "Chaffinch (Fringilla coelebs)",
        population_trends: [
            {term: {start: 1970, end: 2014}, pct_change: 27, annual: 0.55, trend: "no change"},
            {term: {start: 2009, end: 2014}, pct_change: -7, annual: -1.49, trend: "weak decline"}
        ]
    },
    {
        Species: "Song Thrush (Turdus philomelos)",
        population_trends: [
            {term: {start: 1970, end: 2014}, pct_change: -53, annual: -1.7, trend: "weak decline"},
            {term: {start: 2009, end: 2014}, pct_change: -4, annual: -0.88, trend: "no change"}
        ]
    }
]));

// Aggregate tests
const pipeline = [
    {$project: {_id: 0}},
    {$unwind: "$population_trends"},
    {$match: {$expr: {$eq: ["$population_trends.trend", "$$target_trend"]}}},
    {$sort: {Species: 1}}
];
let expectedResults = [{
    Species: "Bullfinch (Pyrrhula pyrrhula)",
    population_trends:
        {term: {start: 2009, end: 2014}, pct_change: 12, annual: 2.38, trend: "weak increase"}
}];
assert.eq(coll.aggregate(pipeline, {let : {target_trend: "weak increase"}}).toArray(),
          expectedResults);

expectedResults = [
    {
        Species: "Chaffinch (Fringilla coelebs)",
        population_trends:
            {term: {start: 2009, end: 2014}, pct_change: -7, annual: -1.49, trend: "weak decline"}
    },
    {
        Species: "Song Thrush (Turdus philomelos)",
        population_trends:
            {term: {start: 1970, end: 2014}, pct_change: -53, annual: -1.7, trend: "weak decline"}
    }
];
assert.eq(coll.aggregate(pipeline, {let : {target_trend: "weak decline"}}).toArray(),
          expectedResults);

// Test that if runtimeConstants and let are both specified, both will coexist.
let constants = {
    localNow: new Date(),
    clusterTime: new Timestamp(0, 0),
};

assert.eq(
    coll.aggregate(pipeline, {runtimeConstants: constants, let : {target_trend: "weak decline"}})
        .toArray(),
    expectedResults);

// Test that undefined let params in the pipeline fail gracefully.
assert.commandFailedWithCode(db.runCommand({
    aggregate: coll.getName(),
    pipeline: pipeline,
    runtimeConstants: constants,
    cursor: {},
    let : {cat: "not_a_bird"}
}),
                             17276);

// Test null and empty let parameters
const pipeline_no_lets = [
    {$project: {_id: 0}},
    {$unwind: "$population_trends"},
    {$match: {$expr: {$eq: ["$population_trends.trend", "weak decline"]}}},
    {$sort: {Species: 1}}
];
assert.eq(coll.aggregate(pipeline_no_lets, {runtimeConstants: constants, let : {}}).toArray(),
          expectedResults);

assert.commandFailedWithCode(db.runCommand({
    aggregate: coll.getName(),
    pipeline: pipeline_no_lets,
    runtimeConstants: constants,
    cursor: {},
    let : null
}),
                             ErrorCodes.TypeMismatch);

// findAndModify
assert.commandWorked(coll.insert({Species: "spy_bird"}));
let result = db.runCommand({
    findAndModify: coll.getName(),
    let : {target_species: "spy_bird"},
    query: {$expr: {$eq: ["$Species", "$$target_species"]}},
    update: {Species: "questionable_bird"},
    fields: {_id: 0},
    new: true
});
assert.eq(result.value, {Species: "questionable_bird"}, result);

result = db.runCommand({
    findAndModify: coll.getName(),
    let : {species_name: "not_a_bird", realSpecies: "dino"},
    query: {$expr: {$eq: ["$Species", "questionable_bird"]}},
    update: [{$project: {Species: "$$species_name"}}, {$addFields: {suspect: "$$realSpecies"}}],
    fields: {_id: 0},
    new: true
});
assert.eq(result.value, {Species: "not_a_bird", suspect: "dino"}, result);

// Delete
result = assert.commandWorked(db.runCommand({
    delete: coll.getName(),
    let : {target_species: "not_a_bird"},
    deletes: [{q: {$expr: {$eq: ["$Species", "$$target_species"]}}, limit: 0}]
}));

// Update
assert.commandWorked(db.runCommand({
    update: coll.getName(),
    let : {target_species: "Song Thrush (Turdus philomelos)", new_name: "Song Thrush"},
    updates: [
        {q: {$expr: {$eq: ["$Species", "$$target_species"]}}, u: [{$set: {Species: "$$new_name"}}]}
    ]
}));

assert.commandWorked(db.runCommand({
    update: coll.getName(),
    let : {target_species: "Song Thrush (Turdus philomelos)"},
    updates: [{
        q: {$expr: {$eq: ["$Species", "$$target_species"]}},
        u: [{$set: {Location: "$$place"}}],
        c: {place: "North America"}
    }]
}));
}());