summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/merge/bypass_doc_validation.js
blob: 957fcc9a2dfeef9ec08e272f71e323c1e9aa190a (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
 * Tests for $merge with bypassDocumentValidation.
 *
 * @tags: [assumes_unsharded_collection]
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // For assertErrorCode.

const testDB = db.getSiblingDB("out_bypass_doc_val");
const sourceColl = testDB.getCollection("source");
const targetColl = testDB.getCollection("target");

targetColl.drop();
assert.commandWorked(testDB.createCollection(targetColl.getName(), {validator: {a: 2}}));

sourceColl.drop();
assert.commandWorked(sourceColl.insert({_id: 0, a: 1}));

// Test that the bypassDocumentValidation flag is passed through to the writes on the output
// collection.
(function testBypassDocValidationTrue() {
    sourceColl.aggregate([{$merge: targetColl.getName()}], {bypassDocumentValidation: true});
    assert.eq([{_id: 0, a: 1}], targetColl.find().toArray());

    sourceColl.aggregate(
        [
            {$addFields: {a: 3}},
            {$merge: {into: targetColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}}
        ],
        {bypassDocumentValidation: true});
    assert.eq([{_id: 0, a: 3}], targetColl.find().toArray());

    sourceColl.aggregate(
        [
            {$replaceRoot: {newRoot: {_id: 1, a: 4}}},
            {$merge: {into: targetColl.getName(), whenMatched: "fail", whenNotMatched: "insert"}}
        ],
        {bypassDocumentValidation: true});
    assert.eq([{_id: 0, a: 3}, {_id: 1, a: 4}], targetColl.find().sort({_id: 1}).toArray());
}());

// Test that mode "replaceDocuments" passes without the bypassDocumentValidation flag if the
// updated doc is valid.
(function testReplacementStyleUpdateWithoutBypass() {
    sourceColl.aggregate([
        {$addFields: {a: 2}},
        {$merge: {into: targetColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}}
    ]);
    assert.eq([{_id: 0, a: 2}], targetColl.find({_id: 0}).toArray());
    sourceColl.aggregate(
        [
            {$addFields: {a: 2}},
            {$merge: {into: targetColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}}
        ],
        {bypassDocumentValidation: false});
    assert.eq([{_id: 0, a: 2}], targetColl.find({_id: 0}).toArray());
}());

function assertDocValidationFailure(cmdOptions) {
    assert.commandWorked(targetColl.remove({}));
    assertErrorCode(sourceColl,
                    [{$merge: targetColl.getName()}],
                    ErrorCodes.DocumentValidationFailure,
                    "Expected failure without bypass set",
                    cmdOptions);

    assertErrorCode(
        sourceColl,
        [
            {$addFields: {a: 3}},
            {$merge: {into: targetColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}}
        ],
        ErrorCodes.DocumentValidationFailure,
        "Expected failure without bypass set",
        cmdOptions);

    assertErrorCode(
        sourceColl,
        [
            {$replaceRoot: {newRoot: {_id: 1, a: 4}}},
            {$merge: {into: targetColl.getName(), whenMatched: "fail", whenNotMatched: "insert"}}
        ],
        ErrorCodes.DocumentValidationFailure,
        "Expected failure without bypass set",
        cmdOptions);
    assert.eq(0, targetColl.find().itcount());
}

// Test that $merge fails if the output document is not valid, and the bypassDocumentValidation
// flag is not set.
assertDocValidationFailure({});

// Test that $merge fails if the output document is not valid, and the bypassDocumentValidation
// flag is explicitly set to false.
assertDocValidationFailure({bypassDocumentValidation: false});

// Test that bypassDocumentValidation is *not* needed if the source collection has a
// validator but the output collection does not.
(function testDocValidatorOnSourceCollection() {
    targetColl.drop();
    assert.commandWorked(testDB.runCommand({collMod: sourceColl.getName(), validator: {a: 1}}));

    sourceColl.aggregate([{$merge: targetColl.getName()}]);
    assert.eq([{_id: 0, a: 1}], targetColl.find().toArray());

    sourceColl.aggregate([
        {$addFields: {a: 3}},
        {$merge: {into: targetColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}}
    ]);
    assert.eq([{_id: 0, a: 3}], targetColl.find().toArray());

    sourceColl.aggregate([
        {$replaceRoot: {newRoot: {_id: 1, a: 4}}},
        {$merge: {into: targetColl.getName(), whenMatched: "fail", whenNotMatched: "insert"}}
    ]);
    assert.eq([{_id: 0, a: 3}, {_id: 1, a: 4}], targetColl.find().sort({_id: 1}).toArray());
}());

// Test that the bypassDocumentValidation is casted to true if the value is non-boolean.
(function testNonBooleanBypassDocValidationFlag() {
    assert.commandWorked(targetColl.remove({}));
    assert.commandWorked(testDB.runCommand({collMod: targetColl.getName(), validator: {a: 1}}));
    sourceColl.drop();
    assert.commandWorked(sourceColl.insert({_id: 0, a: 1}));

    sourceColl.aggregate([{$merge: targetColl.getName()}], {bypassDocumentValidation: 5});
    assert.eq([{_id: 0, a: 1}], targetColl.find().toArray());

    sourceColl.aggregate(
        [
            {$addFields: {a: 3}},
            {$merge: {into: targetColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}}
        ],
        {bypassDocumentValidation: "false"});
    assert.eq([{_id: 0, a: 3}], targetColl.find().toArray());
}());

// Test bypassDocumentValidation with $merge to a collection in a foreign database.
(function testForeignDb() {
    const foreignDB = db.getSiblingDB("foreign_db");
    const foreignColl = foreignDB.foreign_coll;
    foreignColl.drop();
    assert.commandWorked(foreignDB.createCollection(foreignColl.getName(), {validator: {a: 2}}));

    sourceColl.aggregate(
        [
            {$addFields: {a: 3}},
            {
                $merge: {
                    into: {
                        db: foreignDB.getName(),
                        coll: foreignColl.getName(),
                    },
                    whenMatched: "replace",
                    whenNotMatched: "insert"
                }
            }
        ],
        {bypassDocumentValidation: true});
    assert.eq([{_id: 0, a: 3}], foreignColl.find().toArray());

    sourceColl.aggregate(
        [
            {$replaceRoot: {newRoot: {_id: 1, a: 4}}},
            {
                $merge: {
                    into: {
                        db: foreignDB.getName(),
                        coll: foreignColl.getName(),
                    },
                    whenMatched: "fail",
                    whenNotMatched: "insert"
                }
            }
        ],
        {bypassDocumentValidation: true});
    assert.eq([{_id: 0, a: 3}, {_id: 1, a: 4}], foreignColl.find().sort({_id: 1}).toArray());

    assert.commandWorked(foreignColl.remove({}));
    assertErrorCode(sourceColl,
                    [
                        {$addFields: {a: 3}},
                        {
                            $merge: {
                                into: {
                                    db: foreignDB.getName(),
                                    coll: foreignColl.getName(),
                                },
                                whenMatched: "replace",
                                whenNotMatched: "insert"
                            }
                        }
                    ],
                    ErrorCodes.DocumentValidationFailure);

    assertErrorCode(sourceColl,
                    [
                        {$replaceRoot: {newRoot: {_id: 1, a: 4}}},
                        {
                            $merge: {
                                into: {
                                    db: foreignDB.getName(),
                                    coll: foreignColl.getName(),
                                },
                                whenMatched: "fail",
                                whenNotMatched: "insert"
                            }
                        }
                    ],
                    ErrorCodes.DocumentValidationFailure);
    assert.eq(0, foreignColl.find().itcount());
}());
}());