summaryrefslogtreecommitdiff
path: root/jstests/batch_write_command_delete.js
blob: 4f65e249d5d32cff1419d10f253e39b67f223696 (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
//
// Ensures that mongod respects the batch write protocols for delete
//

var coll = db.getCollection( "batch_write_delete" );
coll.drop();

jsTest.log("Starting delete tests...");

var request;
var result;

function resultOK( result ) {
    return result.ok &&
           !( 'code' in result ) &&
           !( 'errmsg' in result ) &&
           !( 'errInfo' in result ) &&
           !( 'errDetails' in result );
};

function resultNOK( result ) {
    return !result.ok &&
           typeof( result.code ) == 'number' &&
           typeof( result.errmsg ) == 'string';
};

// EACH TEST BELOW SHOULD BE SELF-CONTAINED, FOR EASIER DEBUGGING

//
// NO DOCS, illegal command
coll.remove({});
coll.insert({a:1});
printjson( request = {'delete' : coll.getName()} );
printjson( result = coll.runCommand(request) );
assert(resultNOK(result));
assert.eq(1, coll.count());

//
// Single document remove, default write concern specified
coll.remove({});
coll.insert({a:1});
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {a:1}, limit: 1}]} );
printjson( result = coll.runCommand(request) );
assert(resultOK(result));
assert.eq(1, result.n);
assert.eq(0, coll.count());

//
// Single document delete, w:0 write concern specified
coll.remove({});
coll.insert({a:1});
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {a: 1}, limit: 1}],
                      writeConcern:{w:0}} );
printjson( result = coll.runCommand(request) );
assert(resultOK(result));
assert.eq(1, result.n);
assert.eq(0, coll.count());

//
// Single document remove, w:1 write concern specified, ordered:true
coll.remove({});
coll.insert([{a:1}, {a:1}]);
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {a: 1}, limit: 1}],
                      writeConcern:{w:1},
                      ordered: false} );
printjson( result = coll.runCommand(request) );
assert(resultOK(result));
assert.eq(1, result.n);
assert.eq(1, coll.count());

//
// Multiple document remove, w:1 write concern specified, ordered:true, default top
coll.remove({});
coll.insert([{a:1}, {a:1}]);
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {a: 1}, limit: 0}],
                      writeConcern:{w:1},
                      ordered: false} );
printjson( result = coll.runCommand(request) );
assert(resultOK(result));
assert.eq(2, result.n);
assert.eq(0, coll.count());

//
// Multiple document remove, w:1 write concern specified, ordered:true, top:0
coll.remove({});
coll.insert([{a:1}, {a:1}]);
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {a: 1}, limit: 0}],
                      writeConcern:{w:1},
                      ordered: false} );
printjson( result = coll.runCommand(request) );
assert(resultOK(result));
assert.eq(2, result.n);
assert.eq(0, coll.count());

//
// Cause remove error using ordered:true
coll.remove({});
coll.insert({a:1});
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {a:1}, limit: 0},
                                {q: {$set: {a: 1}}, limit: 0},
                                {q: {$set: {a: 1}}, limit: 0}],
                      writeConcern:{w:1},
                      ordered: true} );
printjson( result = coll.runCommand(request) );
assert(resultNOK(result));
assert.eq(1, result.n);
assert.eq(1, result.errDetails.length);

assert.eq(1, result.errDetails[0].index);
assert.eq('number', typeof result.errDetails[0].code);
assert.eq('string', typeof result.errDetails[0].errmsg);
assert.eq(0, coll.count());

//
// Cause remove error using ordered:false
coll.remove({});
coll.insert({a:1});
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {$set: {a: 1}}, limit: 0},
                                {q: {$set: {a: 1}}, limit: 0},
                                {q: {a:1}, limit: 0}],
                      writeConcern:{w:1},
                      ordered: false} );
printjson( result = coll.runCommand(request) );
assert(resultNOK(result));
assert.eq(1, result.n);
assert.eq(2, result.errDetails.length);

assert.eq(0, result.errDetails[0].index);
assert.eq('number', typeof result.errDetails[0].code);
assert.eq('string', typeof result.errDetails[0].errmsg);

assert.eq(1, result.errDetails[1].index);
assert.eq('number', typeof result.errDetails[1].code);
assert.eq('string', typeof result.errDetails[1].errmsg);
assert.eq(0, coll.count());

//
// Cause remove error using ordered:false and w:0
coll.remove({});
coll.insert({a:1});
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {$set: {a: 1}}, limit: 0},
                                {q: {$set: {a: 1}}, limit: 0},
                                {q: {a:1}, limit: 0}],
                      writeConcern:{w:0},
                      ordered: false} );
printjson( result = coll.runCommand(request) );
assert(resultNOK(result));
assert.eq(1, result.n);
assert(!('errDetails' in result));
assert.eq(0, coll.count());

//
// Cause remove error using ordered:true and w:0
coll.remove({});
coll.insert({a:1});
printjson( request = {'delete' : coll.getName(),
                      deletes: [{q: {$set: {a: 1}}, limit: 0},
                                {q: {$set: {a: 1}}, limit: 0},
                                {q: {a:1}, limit:(1)}],
                      writeConcern:{w:0},
                      ordered: true} );
printjson( result = coll.runCommand(request) );
assert(resultNOK(result));
assert.eq(0, result.n);
assert(!('errDetails' in result));
assert.eq(1, coll.count());