Skip to content

Commit 2caeacd

Browse files
authored
RUBY-1860 Standardize on :write_concern for write concern options (#1413)
* Change client and collection from :write to :write_concern * Use :write_concern internally * Switch gridfs code to :write_concern * Fix the docs
1 parent 5501feb commit 2caeacd

File tree

3 files changed

+268
-52
lines changed

3 files changed

+268
-52
lines changed

source/tutorials/ruby-driver-create-client.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ URI Options Conversions
129129
- ``:connect_timeout => Float``
130130

131131
* - fsync=Boolean
132-
- ``{ :write => { :fsync => true|false }}``
132+
- ``{ :write_concern => { :fsync => true|false }}``
133133

134134
* - heartbeatFrequencyMS=Integer
135135
- ``:heartbeat_frequency => Float``
136136

137137
* - journal=Boolean
138-
- ``{ :write => { :j => true|false }}``
138+
- ``{ :write_concern => { :j => true|false }}``
139139

140140
* - localThresholdMS=Integer
141141
- ``:local_threshold => Float``
@@ -217,13 +217,13 @@ URI Options Conversions
217217
is inverted before being used to set ``ssl_verify``.
218218

219219
* - w=Integer|String
220-
- ``{ :write => { :w => Integer|String }}``
220+
- ``{ :write_concern => { :w => Integer|String }}``
221221

222222
* - waitQueueTimeoutMS=Integer
223223
- ``:wait_queue_timeout => Float``
224224

225225
* - wtimeoutMS=Integer
226-
- ``{ :write => { :wtimeout => Float }}``
226+
- ``{ :write_concern => { :wtimeout => Float }}``
227227

228228
* - zlibCompressionLevel=Integer
229229
- ``:zlib_compression_level => Integer``
@@ -543,15 +543,22 @@ Ruby Options
543543
- 1
544544

545545
* - ``:write``
546+
- Deprecated. Equivalent to ``:write_concern`` option. If both ``:write``
547+
and ``:write_concern`` are specified, their values must be identical.
548+
549+
- ``Hash``
550+
- ``{ w: 1 }``
551+
552+
* - ``:write_concern``
546553
- Specifies write concern options as a ``Hash``.
547554
Keys in the hash can be ``:w``, ``:wtimeout``, ``:j``, ``:fsync``.
548555

549556
.. code-block:: ruby
550557

551-
{ :write => { :w => 2 } }
558+
{ write_concern: { w: 2 } }
552559

553560
- ``Hash``
554-
- ``{ :w => 1 }``
561+
- ``{ w: 1 }``
555562

556563
* - ``:zlib_compression_level``
557564
- The Zlib compression level to use, if using compression. See Ruby's Zlib module for valid levels.

source/tutorials/ruby-driver-crud-operations.txt

Lines changed: 171 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -444,18 +444,6 @@ the modification occurs.
444444
)
445445
doc # Return the document after the update.
446446

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-
459447
Deleting
460448
--------
461449

@@ -482,6 +470,177 @@ Deleting
482470
result = artists.delete_many(:label => 'Mute')
483471
result.deleted_count # Returns the number deleted.
484472

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+
485644
A Note about the BSON Symbol type
486645
---------------------------------
487646

0 commit comments

Comments
 (0)