@@ -444,18 +444,6 @@ the modification occurs.
444
444
)
445
445
doc # Return the document after the update.
446
446
447
- Write Concern
448
- ~~~~~~~~~~~~~
449
-
450
- Write concern can be given for specific operations on a collection
451
- using the ``with`` method, similarly to read preference:
452
-
453
- .. code-block:: ruby
454
-
455
- client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
456
- artists = client[:artists]
457
- artists.with(:write => { :w => :3 }).insert_one( { :name => 'Depeche Mode' } )
458
-
459
447
Deleting
460
448
--------
461
449
@@ -482,6 +470,177 @@ Deleting
482
470
result = artists.delete_many(:label => 'Mute')
483
471
result.deleted_count # Returns the number deleted.
484
472
473
+ .. _write-concern:
474
+
475
+ Write Concern
476
+ -------------
477
+
478
+ All write operations in MongoDB are executed with a write concern which is
479
+ the level of acknowledgment requested from MongoDB for the particular write.
480
+ More information about write concerns in general is available in the
481
+ `MongoDB manual <https://docs.mongodb.com/manual/reference/write-concern/>`_.
482
+
483
+ The Ruby driver supports specifying write concern on client, collection,
484
+ session (for transactions on that session), transaction, GridFS bucket
485
+ and write stream levels, as well as when manually issuing commands via
486
+ ``Database#command``.
487
+
488
+ As of driver version 2.10, all driver objects accepting write concerns do so
489
+ through the ``:write_concern`` option, which should be given a hash with
490
+ the write concern options. Usage of the ``:write`` option is deprecated.
491
+ In driver versions 2.9 and below, client, collection and GridFS objects
492
+ took write concern options in the ``:write`` option with session and
493
+ transaction objects employing the ``:write_concern`` option.
494
+
495
+ Below are some examples of passing write concerns to client and colection
496
+ objects. The ``:write_concern`` option can be provided when constructing
497
+ new client and collection objects, or to the ``#with`` methods.
498
+
499
+ GridFS examples are provided on the :ref:`GridFS <gridfs>` page.
500
+
501
+ .. code-block:: ruby
502
+
503
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
504
+ write_concern: {w: 2})
505
+ alt_client = client.with(write_concern: {w: :majority})
506
+
507
+ collection = client[:artists, write_concern: {w: 3}]
508
+ alt_collection = collection.with(write_concern: {w: :majority})
509
+
510
+ # Uses w: 3
511
+ collection.insert_one({name: 'SUN Project'})
512
+ # Uses w: :majority
513
+ alt_collection.insert_one({name: 'SUN Project'})
514
+
515
+ Driver versions 2.9 and earlier accepted write concerns on client and collection
516
+ level via the ``:write`` option. This usage continues to be supported for
517
+ backwards compatibility, but is deprecated:
518
+
519
+ .. code-block:: ruby
520
+
521
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
522
+ write: {w: 2})
523
+ alt_client = client.with(write: {w: :majority})
524
+
525
+ collection = client[:artists, write: {w: 3}]
526
+ alt_collection = collection.with(write: {w: :majority})
527
+
528
+ If both ``:write`` and ``:write_concern`` options are provided, their
529
+ values must be identical or an exception will be raised:
530
+
531
+ .. code-block:: ruby
532
+
533
+ # OK
534
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
535
+ write_concern: {w: 3}, write: {w: 3})
536
+
537
+ # Error
538
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
539
+ write_concern: {w: 3}, write: {w: :majority})
540
+
541
+ When ``#with`` methods are used to alter the options on a client or collection,
542
+ the last provided option wins in case of naming differences:
543
+
544
+ .. code-block:: ruby
545
+
546
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
547
+ write_concern: {w: 2})
548
+ alt_client = client.with(write: {w: 3})
549
+
550
+ alt_client.options[:write]
551
+ # => {"w"=>3}
552
+
553
+ alt_client.options[:write_concern]
554
+ # => nil
555
+
556
+ When using transactions, write concern is only sent to the server in
557
+ ``commit_transaction`` and ``abort_transaction`` operations
558
+ per the `transactions specification
559
+ <https://github.com/mongodb/specifications/blob/master/source/transactions/transactions.rst#writeconcern>`_.
560
+ Write concern may be set via the ``:write_concern`` option in a
561
+ ``with_transaction`` or ``start_transaction`` call, or via
562
+ ``default_transaction_options`` option on a session object.
563
+ If neither of these is set, write concern of the client is used; note
564
+ that transactions ignore write concerns of collections that are involved
565
+ in their operations. Note that when setting the write concern as a
566
+ transaction option, the ``:write`` option is not recognized by any
567
+ driver version.
568
+
569
+ .. code-block:: ruby
570
+
571
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
572
+ write_concern: {w: 2})
573
+ collection = client[:artists, write_concern: {w: :majority}]
574
+
575
+
576
+ session = client.start_session
577
+ session.with_transaction do
578
+ collection.insert_one({test: 1}, session: session)
579
+
580
+ # Uses w: 2 when committing
581
+ end
582
+
583
+
584
+ session = client.start_session(default_transaction_options:
585
+ {write_concern: {w: 3})
586
+ )
587
+ session.with_transaction do
588
+ collection.insert_one({test: 1}, session: session)
589
+
590
+ # Uses w: 3 when committing
591
+ end
592
+
593
+
594
+ session = client.start_session
595
+ session.with_transaction(write_concern: {w: 3}) do
596
+ collection.insert_one({test: 1}, session: session)
597
+
598
+ # Uses w: 3 when committing
599
+ end
600
+
601
+ When write concerns are inherited, inheritance applies to the entire
602
+ write concern hash rather than individual elements. For example, ``j: true``
603
+ is not inherited in the following case:
604
+
605
+ .. code-block:: ruby
606
+
607
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
608
+ write_concern: {w: 1, j: true})
609
+ collection = client[:artists, write_concern: {w: 2}]
610
+
611
+ collection.write_concern.options
612
+ # => #<Mongo::WriteConcern::Acknowledged:0x47289650367880 options={:w=>2}>
613
+
614
+ Although CRUD operations accept an options hash, they currently do not
615
+ recognize the ``:write_concern`` option:
616
+
617
+ .. code-block:: ruby
618
+
619
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
620
+ write_concern: {w: 2})
621
+ collection = client[:artists, write_concern: {w: :majority}]
622
+
623
+ # Still uses w: :majority
624
+ collection.insert_one({name: 'SUN Project'}, write_concern: {w: 1})
625
+
626
+ The easiest workaround for this is to use ``#with`` to obtain a new collection
627
+ instance with the desired write concern:
628
+
629
+ .. code-block:: ruby
630
+
631
+ # Uses w: 1
632
+ collection.with(write_concern: {w: 1}).insert_one(name: 'SUN Project')
633
+
634
+ Write concern can also be manually specified in ``Database#command``:
635
+
636
+ .. code-block:: ruby
637
+
638
+ client.database.command(create: 'foo-collection', writeConcern: {w: :majority})
639
+
640
+ Note that writeConcern here is part of the operation rather than options,
641
+ and the syntax is the camel case one that MongoDB server recognizes, not the
642
+ underscore one that Ruby driver uses.
643
+
485
644
A Note about the BSON Symbol type
486
645
---------------------------------
487
646
0 commit comments