@@ -458,20 +458,77 @@ the modification occurs.
458458 doc = artists.find_one_and_update( { :name => 'José James' }, { '$set' => { :name => 'José' } } )
459459 doc # Return the document before the update.
460460
461- ``find_one_and_replace``
461+ Update Options
462+ ~~~~~~~~~~~~~~
463+
464+ To add options to an update command, specify them as key-value pairs in the options
465+ Hash argument.
462466
463467.. code-block:: ruby
464468
465- doc = artists.find(:name => 'José James').
466- find_one_and_replace( { '$set' => { :name => 'José' } }, :return_document => :after )
467- doc # Return the document after the update.
469+ client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
470+ artists = client[:artists]
468471
469- doc = artists.find_one_and_replace(
470- { :name => 'José James' },
471- { '$set' => { :name => 'José' } },
472- :return_document => :after
472+ artists.indexes.create_one(name: 1)
473+
474+ # Force the server to use the name index to perform this operation
475+ result = artists.update_one(
476+ { :name => 'Goldie' },
477+ { "$inc" => { :plays => 1 } },
478+ { hint: { name: 1 } }
473479 )
474- doc # Return the document after the update.
480+ result.n # Returns 1.
481+
482+ The following is a list of the options that can be added to update operations,
483+ including ``update_one``, ``update_many``, ``replace_one``,
484+ ``find_one_and_delete``, ``find_one_and_update``, and ``find_one_and_replace``.
485+
486+ .. list-table::
487+ :header-rows: 1
488+ :widths: 40 80
489+
490+ * - Option
491+ - Description
492+ * - ``array_filters``
493+ - An Array of filter documents that determine which array elements to modify
494+ for an update operation on an array field.
495+ * - ``bypass_document_validation``
496+ - Whether to skip document-level validation before writing the document.
497+ * - ``collation``
498+ - Specifies a set of rules to use when comparing strings complying with the
499+ conventions of a particular language.
500+ * - ``hint``
501+ - The index to use for this operation. May be specified as a Hash
502+ (e.g. { _id: 1 }) or as a String (e.g. "_id_"). Supported on MongoDB
503+ server versions 4.2 and newer for ``update_one``, ``update_many``, and
504+ ``replace_one`` commands, and on server versions 4.4 and newer for
505+ ``find_one_and_delete``, ``find_one_and_update``, and ``find_one_and_replace``
506+ commands.
507+ * - ``projection``
508+ - The fields to exclude or include in the operation result (only available
509+ on ``find_one_and_delete``, ``find_one_and_replace``, and
510+ ``find_one_and_update`` commands).
511+ * - ``return_document``
512+ - A symbol specifying whether to return the updated document as it was before or
513+ after the update. Potential values are ``:before`` or ``:after``.
514+ (Only available on ``find_one_and_update`` and ``find_one_and_replace`` commands).
515+ * - ``sort``
516+ - How to sort the results of a find and modify command. Specified as a Hash
517+ key-value pair, where the key is the name of the field to sort by, and
518+ the value is either 1 or -1, specifying a sort in ascending or descending
519+ order (only available on ``find_one_and_delete``, ``find_one_and_replace``,
520+ and ``find_one_and_update`` commands).
521+ * - ``session``
522+ - The session to use for this operation.
523+ * - ``upsert``
524+ - Whether to upsert if the document doesn't exist. Cannot be used on
525+ ``find_one_and_delete`` operation.
526+
527+ For more information about update options, see the MongoDB server documentation
528+ on the following commands:
529+
530+ - :manual:`update </reference/command/update>`
531+ - :manual:`findAndModify </reference/command/findAndModify>`
475532
476533Deleting
477534========
@@ -499,6 +556,45 @@ Deleting
499556 result = artists.delete_many(:label => 'Mute')
500557 result.deleted_count # Returns the number deleted.
501558
559+ Delete Options
560+ ~~~~~~~~~~~~~~
561+
562+ To add options to a delete command, specify them as key-value pairs in the
563+ options Hash argument.
564+
565+ .. code-block:: ruby
566+
567+ client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
568+ artists = client[:artists]
569+
570+ artists.indexes.create_one(name: 1)
571+
572+ # Force the server to use the name index to perform this operation
573+ result = artists.find(:name => 'Björk').delete_one(hint: { name: 1 })
574+ result.deleted_count # Returns 1.
575+
576+ The following is a full list of the available options that can be added
577+ to ``delete_one`` and ``delete_many`` operations.
578+
579+ .. list-table::
580+ :header-rows: 1
581+ :widths: 40 80
582+
583+ * - Option
584+ - Description
585+ * - ``collation``
586+ - Specifies a set of rules to use when comparing strings complying with the
587+ conventions of a particular language.
588+ * - ``hint``
589+ - The index to use for this operation. May be specified as a Hash
590+ (e.g. { _id: 1 }) or as a String (e.g. "_id_"). Supported on MongoDB
591+ server versions 4.4 and newer.
592+ * - ``session``
593+ - The session to use for this operation.
594+
595+ For more information about update options, see the MongoDB server documentation
596+ on the :manual:`delete command. </reference/command/delete>`
597+
502598.. _write-concern:
503599
504600Write Concern
@@ -521,7 +617,7 @@ In driver versions 2.9 and below, client, collection and GridFS objects
521617took write concern options in the ``:write`` option with session and
522618transaction objects employing the ``:write_concern`` option.
523619
524- Below are some examples of passing write concerns to client and colection
620+ Below are some examples of passing write concerns to client and collection
525621objects. The ``:write_concern`` option can be provided when constructing
526622new client and collection objects, or to the ``#with`` methods.
527623
@@ -532,10 +628,10 @@ GridFS examples are provided on the :ref:`GridFS <gridfs>` page.
532628 client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
533629 write_concern: {w: 2})
534630 alt_client = client.with(write_concern: {w: :majority})
535-
631+
536632 collection = client[:artists, write_concern: {w: 3}]
537633 alt_collection = collection.with(write_concern: {w: :majority})
538-
634+
539635 # Uses w: 3
540636 collection.insert_one({name: 'SUN Project'})
541637 # Uses w: :majority
@@ -550,7 +646,7 @@ backwards compatibility, but is deprecated:
550646 client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
551647 write: {w: 2})
552648 alt_client = client.with(write: {w: :majority})
553-
649+
554650 collection = client[:artists, write: {w: 3}]
555651 alt_collection = collection.with(write: {w: :majority})
556652
@@ -562,7 +658,7 @@ values must be identical or an exception will be raised:
562658 # OK
563659 client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
564660 write_concern: {w: 3}, write: {w: 3})
565-
661+
566662 # Error
567663 client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
568664 write_concern: {w: 3}, write: {w: :majority})
@@ -575,10 +671,10 @@ the last provided option wins in case of naming differences:
575671 client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
576672 write_concern: {w: 2})
577673 alt_client = client.with(write: {w: 3})
578-
674+
579675 alt_client.options[:write]
580676 # => {"w"=>3}
581-
677+
582678 alt_client.options[:write_concern]
583679 # => nil
584680
@@ -596,34 +692,34 @@ transaction option, the ``:write`` option is not recognized by any
596692driver version.
597693
598694.. code-block:: ruby
599-
695+
600696 client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
601697 write_concern: {w: 2})
602698 collection = client[:artists, write_concern: {w: :majority}]
603-
604-
699+
700+
605701 session = client.start_session
606702 session.with_transaction do
607703 collection.insert_one({test: 1}, session: session)
608-
704+
609705 # Uses w: 2 when committing
610706 end
611-
612-
707+
708+
613709 session = client.start_session(default_transaction_options:
614710 {write_concern: {w: 3})
615711 )
616712 session.with_transaction do
617713 collection.insert_one({test: 1}, session: session)
618-
714+
619715 # Uses w: 3 when committing
620716 end
621-
622-
717+
718+
623719 session = client.start_session
624720 session.with_transaction(write_concern: {w: 3}) do
625721 collection.insert_one({test: 1}, session: session)
626-
722+
627723 # Uses w: 3 when committing
628724 end
629725
@@ -632,36 +728,36 @@ write concern hash rather than individual elements. For example, ``j: true``
632728is not inherited in the following case:
633729
634730.. code-block:: ruby
635-
731+
636732 client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
637733 write_concern: {w: 1, j: true})
638734 collection = client[:artists, write_concern: {w: 2}]
639-
735+
640736 collection.write_concern.options
641737 # => #<Mongo::WriteConcern::Acknowledged:0x47289650367880 options={:w=>2}>
642738
643739Although CRUD operations accept an options hash, they currently do not
644740recognize the ``:write_concern`` option:
645-
741+
646742.. code-block:: ruby
647743
648744 client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music',
649745 write_concern: {w: 2})
650746 collection = client[:artists, write_concern: {w: :majority}]
651-
747+
652748 # Still uses w: :majority
653749 collection.insert_one({name: 'SUN Project'}, write_concern: {w: 1})
654750
655751The easiest workaround for this is to use ``#with`` to obtain a new collection
656752instance with the desired write concern:
657-
753+
658754.. code-block:: ruby
659755
660756 # Uses w: 1
661757 collection.with(write_concern: {w: 1}).insert_one(name: 'SUN Project')
662758
663759Write concern can also be manually specified in ``Database#command``:
664-
760+
665761.. code-block:: ruby
666762
667763 client.database.command(create: 'foo-collection', writeConcern: {w: :majority})
@@ -673,7 +769,7 @@ underscore one that Ruby driver uses.
673769A Note about the BSON Symbol type
674770=================================
675771
676- Because the BSON specification deprecated the BSON symbol type, the `bson` gem
772+ Because the BSON specification deprecated the BSON symbol type, the `bson` gem
677773will serialize Ruby symbols into BSON strings when used on its own. However, in
678774order to maintain backwards compatibility with older datasets, the Ruby driver
679775overrides this behavior to serialize Ruby symbols as BSON symbols. This is
0 commit comments