mongo 2.2.2 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Rakefile +155 -0
- data/lib/csasl/csasl.bundle +0 -0
- data/lib/mongo/bulk_write/result.rb +1 -1
- data/lib/mongo/bulk_write/result_combiner.rb +8 -5
- data/lib/mongo/cluster/topology/replica_set.rb +2 -1
- data/lib/mongo/error.rb +5 -0
- data/lib/mongo/error/parser.rb +12 -1
- data/lib/mongo/operation/result.rb +2 -2
- data/lib/mongo/operation/write/bulk/delete/result.rb +5 -1
- data/lib/mongo/operation/write/bulk/update/result.rb +38 -7
- data/lib/mongo/protocol/reply.rb +12 -0
- data/lib/mongo/server/description.rb +1 -2
- data/lib/mongo/uri.rb +4 -3
- data/lib/mongo/version.rb +1 -1
- data/spec/mongo/bulk_write_spec.rb +648 -21
- data/spec/mongo/cluster/topology/replica_set_spec.rb +2 -0
- data/spec/mongo/operation/result_spec.rb +15 -0
- data/spec/mongo/uri_spec.rb +9 -1
- data/spec/support/sdam/rs/primary_reports_new_member.yml +163 -0
- metadata +6 -5
- metadata.gz.sig +0 -0
- data/spec/support/shared/bulk_write.rb +0 -731
data/lib/mongo/version.rb
CHANGED
@@ -8,7 +8,7 @@ describe Mongo::BulkWrite do
|
|
8
8
|
|
9
9
|
after do
|
10
10
|
authorized_collection.delete_many
|
11
|
-
collection_with_validator.drop
|
11
|
+
collection_with_validator.drop
|
12
12
|
end
|
13
13
|
|
14
14
|
let(:collection_with_validator) do
|
@@ -18,6 +18,10 @@ describe Mongo::BulkWrite do
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
let(:collection_invalid_write_concern) do
|
22
|
+
authorized_collection.client.with(write: { w: (WRITE_CONCERN[:w] + 1) })[authorized_collection.name]
|
23
|
+
end
|
24
|
+
|
21
25
|
describe '#execute' do
|
22
26
|
|
23
27
|
shared_examples_for 'an executable bulk write' do
|
@@ -81,6 +85,32 @@ describe Mongo::BulkWrite do
|
|
81
85
|
expect(result.inserted_count).to eq(1)
|
82
86
|
expect(authorized_collection.find(_id: 0).count).to eq(1)
|
83
87
|
end
|
88
|
+
|
89
|
+
it 'only inserts that document' do
|
90
|
+
result
|
91
|
+
expect(authorized_collection.find.first['_id']).to eq(0)
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'when there is a write concern error' do
|
95
|
+
|
96
|
+
context 'when the server version is < 2.6' do
|
97
|
+
|
98
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
99
|
+
expect {
|
100
|
+
bulk_write_invalid_write_concern.execute
|
101
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'when the server version has write commands enabled' do
|
106
|
+
|
107
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
108
|
+
expect {
|
109
|
+
bulk_write_invalid_write_concern.execute
|
110
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
84
114
|
end
|
85
115
|
|
86
116
|
context 'when provided multiple insert ones' do
|
@@ -101,6 +131,40 @@ describe Mongo::BulkWrite do
|
|
101
131
|
expect(result.inserted_count).to eq(3)
|
102
132
|
expect(authorized_collection.find(_id: { '$in'=> [ 0, 1, 2 ]}).count).to eq(3)
|
103
133
|
end
|
134
|
+
|
135
|
+
context 'when there is a write failure' do
|
136
|
+
|
137
|
+
let(:requests) do
|
138
|
+
[{ insert_one: { _id: 1 }}, { insert_one: { _id: 1 }}]
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'raises a BulkWriteError' do
|
142
|
+
expect {
|
143
|
+
bulk_write.execute
|
144
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context 'when there is a write concern error' do
|
149
|
+
|
150
|
+
context 'when the server version is < 2.6' do
|
151
|
+
|
152
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
153
|
+
expect {
|
154
|
+
bulk_write_invalid_write_concern.execute
|
155
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
context 'when the server version has write commands enabled' do
|
160
|
+
|
161
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
162
|
+
expect {
|
163
|
+
bulk_write_invalid_write_concern.execute
|
164
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
104
168
|
end
|
105
169
|
|
106
170
|
context 'when provided a single delete one' do
|
@@ -121,6 +185,47 @@ describe Mongo::BulkWrite do
|
|
121
185
|
expect(result.deleted_count).to eq(1)
|
122
186
|
expect(authorized_collection.find(_id: 0).count).to eq(0)
|
123
187
|
end
|
188
|
+
|
189
|
+
context 'when there is a write concern error' do
|
190
|
+
|
191
|
+
context 'when the server version is < 2.6' do
|
192
|
+
|
193
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
194
|
+
expect {
|
195
|
+
bulk_write_invalid_write_concern.execute
|
196
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context 'when the server version has write commands enabled' do
|
201
|
+
|
202
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
203
|
+
expect {
|
204
|
+
bulk_write_invalid_write_concern.execute
|
205
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'when multiple documents match delete selector' do
|
211
|
+
|
212
|
+
before do
|
213
|
+
authorized_collection.insert_many([{ a: 1 }, { a: 1 }])
|
214
|
+
end
|
215
|
+
|
216
|
+
let(:requests) do
|
217
|
+
[{ delete_one: { filter: { a: 1 }}}]
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'reports n_removed correctly' do
|
221
|
+
expect(bulk_write.execute.deleted_count).to eq(1)
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'deletes only matching documents' do
|
225
|
+
bulk_write.execute
|
226
|
+
expect(authorized_collection.find(a: 1).count).to eq(1)
|
227
|
+
end
|
228
|
+
end
|
124
229
|
end
|
125
230
|
|
126
231
|
context 'when provided multiple delete ones' do
|
@@ -147,6 +252,27 @@ describe Mongo::BulkWrite do
|
|
147
252
|
expect(result.deleted_count).to eq(3)
|
148
253
|
expect(authorized_collection.find(_id: { '$in'=> [ 0, 1, 2 ]}).count).to eq(0)
|
149
254
|
end
|
255
|
+
|
256
|
+
context 'when there is a write concern error' do
|
257
|
+
|
258
|
+
context 'when the server version is < 2.6' do
|
259
|
+
|
260
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
261
|
+
expect {
|
262
|
+
bulk_write_invalid_write_concern.execute
|
263
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
context 'when the server version has write commands enabled' do
|
268
|
+
|
269
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
270
|
+
expect {
|
271
|
+
bulk_write_invalid_write_concern.execute
|
272
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
150
276
|
end
|
151
277
|
|
152
278
|
context 'when provided a single delete many' do
|
@@ -167,6 +293,27 @@ describe Mongo::BulkWrite do
|
|
167
293
|
expect(result.deleted_count).to eq(1)
|
168
294
|
expect(authorized_collection.find(_id: 0).count).to eq(0)
|
169
295
|
end
|
296
|
+
|
297
|
+
context 'when there is a write concern error' do
|
298
|
+
|
299
|
+
context 'when the server version is < 2.6' do
|
300
|
+
|
301
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
302
|
+
expect {
|
303
|
+
bulk_write_invalid_write_concern.execute
|
304
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
context 'when the server version has write commands enabled' do
|
309
|
+
|
310
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
311
|
+
expect {
|
312
|
+
bulk_write_invalid_write_concern.execute
|
313
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
170
317
|
end
|
171
318
|
|
172
319
|
context 'when provided multiple delete many ops' do
|
@@ -193,6 +340,27 @@ describe Mongo::BulkWrite do
|
|
193
340
|
expect(result.deleted_count).to eq(3)
|
194
341
|
expect(authorized_collection.find(_id: { '$in'=> [ 0, 1, 2 ]}).count).to eq(0)
|
195
342
|
end
|
343
|
+
|
344
|
+
context 'when there is a write concern error' do
|
345
|
+
|
346
|
+
context 'when the server version is < 2.6' do
|
347
|
+
|
348
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
349
|
+
expect {
|
350
|
+
bulk_write_invalid_write_concern.execute
|
351
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
context 'when the server version has write commands enabled' do
|
356
|
+
|
357
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
358
|
+
expect {
|
359
|
+
bulk_write_invalid_write_concern.execute
|
360
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
361
|
+
end
|
362
|
+
end
|
363
|
+
end
|
196
364
|
end
|
197
365
|
|
198
366
|
context 'when providing a single replace one' do
|
@@ -213,45 +381,492 @@ describe Mongo::BulkWrite do
|
|
213
381
|
expect(result.modified_count).to eq(1)
|
214
382
|
expect(authorized_collection.find(_id: 0).first[:name]).to eq('test')
|
215
383
|
end
|
384
|
+
|
385
|
+
context 'when there is a write concern error' do
|
386
|
+
|
387
|
+
context 'when the server version is < 2.6' do
|
388
|
+
|
389
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
390
|
+
expect {
|
391
|
+
bulk_write_invalid_write_concern.execute
|
392
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
context 'when the server version has write commands enabled' do
|
397
|
+
|
398
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
399
|
+
expect {
|
400
|
+
bulk_write_invalid_write_concern.execute
|
401
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
216
405
|
end
|
217
406
|
|
218
407
|
context 'when providing a single update one' do
|
219
408
|
|
220
|
-
|
221
|
-
|
409
|
+
context 'when upsert is false' do
|
410
|
+
|
411
|
+
let(:requests) do
|
412
|
+
[{ update_one: { filter: { _id: 0 }, update: { "$set" => { name: 'test' }}}}]
|
413
|
+
end
|
414
|
+
|
415
|
+
let(:result) do
|
416
|
+
bulk_write.execute
|
417
|
+
end
|
418
|
+
|
419
|
+
before do
|
420
|
+
authorized_collection.insert_one({ _id: 0 })
|
421
|
+
end
|
422
|
+
|
423
|
+
it 'updates the document' do
|
424
|
+
result
|
425
|
+
expect(authorized_collection.find(_id: 0).first[:name]).to eq('test')
|
426
|
+
end
|
427
|
+
|
428
|
+
it 'reports the upserted id' do
|
429
|
+
expect(result.upserted_ids).to eq([])
|
430
|
+
end
|
431
|
+
|
432
|
+
it 'reports the upserted count' do
|
433
|
+
expect(result.upserted_count).to eq(0)
|
434
|
+
end
|
435
|
+
|
436
|
+
it 'reports the modified count' do
|
437
|
+
expect(result.modified_count).to eq(1)
|
438
|
+
end
|
439
|
+
|
440
|
+
it 'reports the matched count' do
|
441
|
+
expect(result.matched_count).to eq(1)
|
442
|
+
end
|
443
|
+
|
444
|
+
context 'when documents match but are not modified' do
|
445
|
+
|
446
|
+
before do
|
447
|
+
authorized_collection.insert_one({ a: 0 })
|
448
|
+
end
|
449
|
+
|
450
|
+
let(:requests) do
|
451
|
+
[{ update_one: { filter: { a: 0 }, update: { "$set" => { a: 0 }}}}]
|
452
|
+
end
|
453
|
+
|
454
|
+
it 'reports the upserted id' do
|
455
|
+
expect(result.upserted_ids).to eq([])
|
456
|
+
end
|
457
|
+
|
458
|
+
it 'reports the upserted count' do
|
459
|
+
expect(result.upserted_count).to eq(0)
|
460
|
+
end
|
461
|
+
|
462
|
+
it 'reports the modified count', if: write_command_enabled? do
|
463
|
+
expect(result.modified_count).to eq(0)
|
464
|
+
end
|
465
|
+
|
466
|
+
it 'reports the matched count' do
|
467
|
+
expect(result.matched_count).to eq(1)
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
context 'when the number of updates exceeds the max batch size', if: write_command_enabled? do
|
472
|
+
|
473
|
+
let(:requests) do
|
474
|
+
1001.times.collect do |i|
|
475
|
+
{ update_one: { filter: { a: i }, update: { "$set" => { a: i, b: 3 }}, upsert: true }}
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
it 'updates the documents and reports the correct number of upserted ids' do
|
480
|
+
expect(result.upserted_ids.size).to eq(1001)
|
481
|
+
expect(authorized_collection.find(b: 3).count).to eq(1001)
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
context 'when there is a write concern error' do
|
486
|
+
|
487
|
+
context 'when the server version is < 2.6' do
|
488
|
+
|
489
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
490
|
+
expect {
|
491
|
+
bulk_write_invalid_write_concern.execute
|
492
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
493
|
+
end
|
494
|
+
end
|
495
|
+
|
496
|
+
context 'when the server version has write commands enabled' do
|
497
|
+
|
498
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
499
|
+
expect {
|
500
|
+
bulk_write_invalid_write_concern.execute
|
501
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
502
|
+
end
|
503
|
+
end
|
504
|
+
end
|
222
505
|
end
|
223
506
|
|
224
|
-
|
225
|
-
|
507
|
+
context 'when upsert is true' do
|
508
|
+
|
509
|
+
let(:requests) do
|
510
|
+
[{ update_one: { filter: { _id: 0 }, update: { "$set" => { name: 'test' } }, upsert: true }}]
|
511
|
+
end
|
512
|
+
|
513
|
+
let(:result) do
|
514
|
+
bulk_write.execute
|
515
|
+
end
|
516
|
+
|
517
|
+
it 'updates the document' do
|
518
|
+
result
|
519
|
+
expect(authorized_collection.find(_id: 0).first[:name]).to eq('test')
|
520
|
+
end
|
521
|
+
|
522
|
+
it 'reports the upserted count' do
|
523
|
+
expect(result.upserted_count).to eq(1)
|
524
|
+
end
|
525
|
+
|
526
|
+
it 'reports the matched count' do
|
527
|
+
expect(result.modified_count).to eq(0)
|
528
|
+
end
|
529
|
+
|
530
|
+
it 'reports the modified count' do
|
531
|
+
expect(result.modified_count).to eq(0)
|
532
|
+
end
|
533
|
+
|
534
|
+
it 'reports the upserted id', if: write_command_enabled? do
|
535
|
+
expect(result.upserted_ids).to eq([0])
|
536
|
+
end
|
537
|
+
|
538
|
+
context 'when there is a write concern error' do
|
539
|
+
|
540
|
+
context 'when the server version is < 2.6' do
|
541
|
+
|
542
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
543
|
+
expect {
|
544
|
+
bulk_write_invalid_write_concern.execute
|
545
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
546
|
+
end
|
547
|
+
end
|
548
|
+
|
549
|
+
context 'when the server version has write commands enabled' do
|
550
|
+
|
551
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
552
|
+
expect {
|
553
|
+
bulk_write_invalid_write_concern.execute
|
554
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
555
|
+
end
|
556
|
+
end
|
557
|
+
end
|
226
558
|
end
|
559
|
+
end
|
227
560
|
|
228
|
-
|
229
|
-
|
561
|
+
context 'when providing multiple update ones' do
|
562
|
+
|
563
|
+
context 'when upsert is false' do
|
564
|
+
|
565
|
+
let(:requests) do
|
566
|
+
[{ update_one: { filter: { _id: 0 }, update: { "$set" => { name: 'test' }}}},
|
567
|
+
{ update_one: { filter: { _id: 1 }, update: { "$set" => { name: 'test' }}}}]
|
568
|
+
end
|
569
|
+
|
570
|
+
let(:result) do
|
571
|
+
bulk_write.execute
|
572
|
+
end
|
573
|
+
|
574
|
+
before do
|
575
|
+
authorized_collection.insert_many([{ _id: 0 }, { _id: 1 }])
|
576
|
+
end
|
577
|
+
|
578
|
+
it 'updates the document' do
|
579
|
+
result
|
580
|
+
expect(authorized_collection.find(name: 'test').count).to eq(2)
|
581
|
+
end
|
582
|
+
|
583
|
+
it 'reports the upserted id' do
|
584
|
+
expect(result.upserted_ids).to eq([])
|
585
|
+
end
|
586
|
+
|
587
|
+
it 'reports the upserted count' do
|
588
|
+
expect(result.upserted_count).to eq(0)
|
589
|
+
end
|
590
|
+
|
591
|
+
it 'reports the modified count' do
|
592
|
+
expect(result.modified_count).to eq(2)
|
593
|
+
end
|
594
|
+
|
595
|
+
it 'reports the matched count' do
|
596
|
+
expect(result.modified_count).to eq(2)
|
597
|
+
end
|
598
|
+
|
599
|
+
|
600
|
+
context 'when there is a mix of updates and matched without an update' do
|
601
|
+
|
602
|
+
let(:requests) do
|
603
|
+
[{ update_one: { filter: { a: 0 }, update: { "$set" => { a: 1 }}}},
|
604
|
+
{ update_one: { filter: { a: 2 }, update: { "$set" => { a: 2 }}}}]
|
605
|
+
end
|
606
|
+
|
607
|
+
let(:result) do
|
608
|
+
bulk_write.execute
|
609
|
+
end
|
610
|
+
|
611
|
+
before do
|
612
|
+
authorized_collection.insert_many([{ a: 0 }, { a: 2 }])
|
613
|
+
end
|
614
|
+
|
615
|
+
it 'updates the document' do
|
616
|
+
result
|
617
|
+
expect(authorized_collection.find(a: { '$lt' => 3 }).count).to eq(2)
|
618
|
+
end
|
619
|
+
|
620
|
+
it 'reports the upserted id' do
|
621
|
+
expect(result.upserted_ids).to eq([])
|
622
|
+
end
|
623
|
+
|
624
|
+
it 'reports the upserted count' do
|
625
|
+
expect(result.upserted_count).to eq(0)
|
626
|
+
end
|
627
|
+
|
628
|
+
it 'reports the modified count', if: write_command_enabled? do
|
629
|
+
expect(result.modified_count).to eq(1)
|
630
|
+
end
|
631
|
+
|
632
|
+
it 'reports the modified count', unless: write_command_enabled? do
|
633
|
+
expect(result.modified_count).to eq(2)
|
634
|
+
end
|
635
|
+
|
636
|
+
it 'reports the matched count' do
|
637
|
+
expect(result.matched_count).to eq(2)
|
638
|
+
end
|
639
|
+
end
|
640
|
+
|
641
|
+
context 'when there is a write concern error' do
|
642
|
+
|
643
|
+
context 'when the server version is < 2.6' do
|
644
|
+
|
645
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
646
|
+
expect {
|
647
|
+
bulk_write_invalid_write_concern.execute
|
648
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
649
|
+
end
|
650
|
+
end
|
651
|
+
|
652
|
+
context 'when the server version has write commands enabled' do
|
653
|
+
|
654
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
655
|
+
expect {
|
656
|
+
bulk_write_invalid_write_concern.execute
|
657
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
658
|
+
end
|
659
|
+
end
|
660
|
+
end
|
230
661
|
end
|
231
662
|
|
232
|
-
|
233
|
-
|
234
|
-
|
663
|
+
context 'when upsert is true' do
|
664
|
+
|
665
|
+
let(:requests) do
|
666
|
+
[{ update_one: { filter: { _id: 0 }, update: { "$set" => { name: 'test' }}, upsert: true }},
|
667
|
+
{ update_one: { filter: { _id: 1 }, update: { "$set" => { name: 'test1' }}, upsert: true }}]
|
668
|
+
end
|
669
|
+
|
670
|
+
let(:result) do
|
671
|
+
bulk_write.execute
|
672
|
+
end
|
673
|
+
|
674
|
+
it 'updates the document' do
|
675
|
+
expect(result.modified_count).to eq(0)
|
676
|
+
expect(authorized_collection.find(name: { '$in' => ['test', 'test1'] }).count).to eq(2)
|
677
|
+
end
|
678
|
+
|
679
|
+
it 'reports the upserted count' do
|
680
|
+
expect(result.upserted_count).to eq(2)
|
681
|
+
end
|
682
|
+
|
683
|
+
it 'reports the modified count' do
|
684
|
+
expect(result.modified_count).to eq(0)
|
685
|
+
end
|
686
|
+
|
687
|
+
it 'reports the matched count' do
|
688
|
+
expect(result.matched_count).to eq(0)
|
689
|
+
end
|
690
|
+
|
691
|
+
it 'reports the upserted id', if: write_command_enabled? do
|
692
|
+
expect(result.upserted_ids).to eq([0, 1])
|
693
|
+
end
|
694
|
+
|
695
|
+
context 'when there is a mix of updates, upsert, and matched without an update' do
|
696
|
+
|
697
|
+
let(:requests) do
|
698
|
+
[{ update_one: { filter: { a: 0 }, update: { "$set" => { a: 1 }}}},
|
699
|
+
{ update_one: { filter: { a: 2 }, update: { "$set" => { a: 2 }}}},
|
700
|
+
{ update_one: { filter: { _id: 3 }, update: { "$set" => { a: 4 }}, upsert: true }}]
|
701
|
+
end
|
702
|
+
|
703
|
+
let(:result) do
|
704
|
+
bulk_write.execute
|
705
|
+
end
|
706
|
+
|
707
|
+
before do
|
708
|
+
authorized_collection.insert_many([{ a: 0 }, { a: 2 }])
|
709
|
+
end
|
710
|
+
|
711
|
+
it 'updates the documents' do
|
712
|
+
result
|
713
|
+
expect(authorized_collection.find(a: { '$lt' => 3 }).count).to eq(2)
|
714
|
+
expect(authorized_collection.find(a: 4).count).to eq(1)
|
715
|
+
end
|
716
|
+
|
717
|
+
it 'reports the upserted id', if: write_command_enabled? do
|
718
|
+
expect(result.upserted_ids).to eq([3])
|
719
|
+
end
|
720
|
+
|
721
|
+
it 'reports the upserted count' do
|
722
|
+
expect(result.upserted_count).to eq(1)
|
723
|
+
end
|
724
|
+
|
725
|
+
it 'reports the modified count', if: write_command_enabled? do
|
726
|
+
expect(result.modified_count).to eq(1)
|
727
|
+
end
|
728
|
+
|
729
|
+
it 'reports the modified count', unless: write_command_enabled? do
|
730
|
+
expect(result.modified_count).to eq(2)
|
731
|
+
end
|
732
|
+
|
733
|
+
it 'reports the matched count' do
|
734
|
+
expect(result.matched_count).to eq(2)
|
735
|
+
end
|
736
|
+
end
|
737
|
+
|
738
|
+
context 'when there is a write concern error' do
|
739
|
+
|
740
|
+
context 'when the server version is < 2.6' do
|
741
|
+
|
742
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
743
|
+
expect {
|
744
|
+
bulk_write_invalid_write_concern.execute
|
745
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
746
|
+
end
|
747
|
+
end
|
748
|
+
|
749
|
+
context 'when the server version has write commands enabled' do
|
750
|
+
|
751
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
752
|
+
expect {
|
753
|
+
bulk_write_invalid_write_concern.execute
|
754
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
755
|
+
end
|
756
|
+
end
|
757
|
+
end
|
235
758
|
end
|
236
759
|
end
|
237
760
|
|
238
761
|
context 'when providing a single update many' do
|
239
762
|
|
240
|
-
|
241
|
-
[{ update_many: { filter: { _id: 0 }, update: { "$set" => { name: 'test' }}}}]
|
242
|
-
end
|
763
|
+
context 'when upsert is false' do
|
243
764
|
|
244
|
-
|
245
|
-
|
246
|
-
|
765
|
+
let(:requests) do
|
766
|
+
[{ update_many: { filter: { a: 0 }, update: { "$set" => { name: 'test' }}}}]
|
767
|
+
end
|
247
768
|
|
248
|
-
|
249
|
-
|
769
|
+
let(:result) do
|
770
|
+
bulk_write.execute
|
771
|
+
end
|
772
|
+
|
773
|
+
before do
|
774
|
+
authorized_collection.insert_many([{ a: 0 }, { a: 0 }])
|
775
|
+
end
|
776
|
+
|
777
|
+
it 'updates the documents' do
|
778
|
+
expect(authorized_collection.find(a: 0).count).to eq(2)
|
779
|
+
end
|
780
|
+
|
781
|
+
it 'reports the upserted ids' do
|
782
|
+
expect(result.upserted_ids).to eq([])
|
783
|
+
end
|
784
|
+
|
785
|
+
it 'reports the upserted count' do
|
786
|
+
expect(result.upserted_count).to eq(0)
|
787
|
+
end
|
788
|
+
|
789
|
+
it 'reports the modified count' do
|
790
|
+
expect(result.modified_count).to eq(2)
|
791
|
+
end
|
792
|
+
|
793
|
+
it 'reports the matched count' do
|
794
|
+
expect(result.modified_count).to eq(2)
|
795
|
+
end
|
796
|
+
|
797
|
+
context 'when there is a write concern error' do
|
798
|
+
|
799
|
+
context 'when the server version is < 2.6' do
|
800
|
+
|
801
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
802
|
+
expect {
|
803
|
+
bulk_write_invalid_write_concern.execute
|
804
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
805
|
+
end
|
806
|
+
end
|
807
|
+
|
808
|
+
context 'when the server version has write commands enabled' do
|
809
|
+
|
810
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
811
|
+
expect {
|
812
|
+
bulk_write_invalid_write_concern.execute
|
813
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
814
|
+
end
|
815
|
+
end
|
816
|
+
end
|
250
817
|
end
|
251
818
|
|
252
|
-
|
253
|
-
|
254
|
-
|
819
|
+
context 'when upsert is true' do
|
820
|
+
|
821
|
+
let(:requests) do
|
822
|
+
[{ update_many: { filter: { _id: 0 }, update: { "$set" => { name: 'test' }}, upsert: true }}]
|
823
|
+
end
|
824
|
+
|
825
|
+
let(:result) do
|
826
|
+
bulk_write.execute
|
827
|
+
end
|
828
|
+
|
829
|
+
it 'updates the document' do
|
830
|
+
result
|
831
|
+
expect(authorized_collection.find(name: 'test').count).to eq(1)
|
832
|
+
end
|
833
|
+
|
834
|
+
it 'reports the upserted count' do
|
835
|
+
expect(result.upserted_count).to eq(1)
|
836
|
+
end
|
837
|
+
|
838
|
+
it 'reports the matched count' do
|
839
|
+
expect(result.matched_count).to eq(0)
|
840
|
+
end
|
841
|
+
|
842
|
+
it 'reports the modified count' do
|
843
|
+
expect(result.modified_count).to eq(0)
|
844
|
+
end
|
845
|
+
|
846
|
+
it 'reports the upserted id', if: write_command_enabled? do
|
847
|
+
expect(result.upserted_ids).to eq([0])
|
848
|
+
end
|
849
|
+
|
850
|
+
context 'when there is a write concern error' do
|
851
|
+
|
852
|
+
context 'when the server version is < 2.6' do
|
853
|
+
|
854
|
+
it 'raises a BulkWriteError', if: !write_command_enabled? && standalone? do
|
855
|
+
expect {
|
856
|
+
bulk_write_invalid_write_concern.execute
|
857
|
+
}.to raise_error(Mongo::Error::BulkWriteError)
|
858
|
+
end
|
859
|
+
end
|
860
|
+
|
861
|
+
context 'when the server version has write commands enabled' do
|
862
|
+
|
863
|
+
it 'raises an OperationFailure', if: write_command_enabled? && standalone? do
|
864
|
+
expect {
|
865
|
+
bulk_write_invalid_write_concern.execute
|
866
|
+
}.to raise_error(Mongo::Error::OperationFailure)
|
867
|
+
end
|
868
|
+
end
|
869
|
+
end
|
255
870
|
end
|
256
871
|
end
|
257
872
|
end
|
@@ -302,6 +917,10 @@ describe Mongo::BulkWrite do
|
|
302
917
|
it 'inserts the documents' do
|
303
918
|
expect(result.inserted_count).to eq(1001)
|
304
919
|
end
|
920
|
+
|
921
|
+
it 'combines the inserted ids' do
|
922
|
+
expect(result.inserted_ids.size).to eq(1001)
|
923
|
+
end
|
305
924
|
end
|
306
925
|
end
|
307
926
|
|
@@ -329,6 +948,10 @@ describe Mongo::BulkWrite do
|
|
329
948
|
described_class.new(authorized_collection, requests, ordered: false)
|
330
949
|
end
|
331
950
|
|
951
|
+
let(:bulk_write_invalid_write_concern) do
|
952
|
+
described_class.new(collection_invalid_write_concern, requests, ordered: false)
|
953
|
+
end
|
954
|
+
|
332
955
|
it_behaves_like 'an executable bulk write'
|
333
956
|
end
|
334
957
|
|
@@ -338,6 +961,10 @@ describe Mongo::BulkWrite do
|
|
338
961
|
described_class.new(authorized_collection, requests, ordered: true)
|
339
962
|
end
|
340
963
|
|
964
|
+
let(:bulk_write_invalid_write_concern) do
|
965
|
+
described_class.new(collection_invalid_write_concern, requests, ordered: true)
|
966
|
+
end
|
967
|
+
|
341
968
|
it_behaves_like 'an executable bulk write'
|
342
969
|
end
|
343
970
|
end
|