Skip to content

Commit 93e772d

Browse files
author
Emily Giurleo
authored
RUBY-1972 RUBY-2001 RUBY-2148 Hint documentation (#1948)
* fix typo * add hint option to API docs * add a documentation section for delete options * added hint to API docs in collection methods * write hint documentation * fix update section * try to fix headings * actually fix headings * add information about which server versions support hints * add return_document info and key value --> key-value * make hint examples clearer
1 parent 87cc6c2 commit 93e772d

File tree

3 files changed

+161
-33
lines changed

3 files changed

+161
-33
lines changed

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

Lines changed: 129 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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

476533
Deleting
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

504600
Write Concern
@@ -521,7 +617,7 @@ In driver versions 2.9 and below, client, collection and GridFS objects
521617
took write concern options in the ``:write`` option with session and
522618
transaction 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
525621
objects. The ``:write_concern`` option can be provided when constructing
526622
new 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
596692
driver 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``
632728
is 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

643739
Although CRUD operations accept an options hash, they currently do not
644740
recognize 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

655751
The easiest workaround for this is to use ``#with`` to obtain a new collection
656752
instance 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

663759
Write 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.
673769
A 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
677773
will serialize Ruby symbols into BSON strings when used on its own. However, in
678774
order to maintain backwards compatibility with older datasets, the Ruby driver
679775
overrides this behavior to serialize Ruby symbols as BSON symbols. This is

lib/mongo/collection.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ def bulk_write(requests, options = {})
605605
#
606606
# @option options [ Hash ] :collation The collation to use.
607607
# @option options [ Session ] :session The session to use.
608+
# @option options [ Hash | String ] :hint The index to use for this operation.
609+
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
608610
#
609611
# @return [ Result ] The response from the database.
610612
#
@@ -623,6 +625,8 @@ def delete_one(filter = nil, options = {})
623625
#
624626
# @option options [ Hash ] :collation The collation to use.
625627
# @option options [ Session ] :session The session to use.
628+
# @option options [ Hash | String ] :hint The index to use for this operation.
629+
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
626630
#
627631
# @return [ Result ] The response from the database.
628632
#
@@ -669,6 +673,8 @@ def parallel_scan(cursor_count, options = {})
669673
# not to skip document level validation.
670674
# @option options [ Hash ] :collation The collation to use.
671675
# @option options [ Session ] :session The session to use.
676+
# @option options [ Hash | String ] :hint The index to use for this operation.
677+
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
672678
#
673679
# @return [ Result ] The response from the database.
674680
#
@@ -694,6 +700,8 @@ def replace_one(filter, replacement, options = {})
694700
# @option options [ Array ] :array_filters A set of filters specifying to which array elements
695701
# an update should apply.
696702
# @option options [ Session ] :session The session to use.
703+
# @option options [ Hash | String ] :hint The index to use for this operation.
704+
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
697705
#
698706
# @return [ Result ] The response from the database.
699707
#
@@ -719,6 +727,8 @@ def update_many(filter, update, options = {})
719727
# @option options [ Array ] :array_filters A set of filters specifying to which array elements
720728
# an update should apply.
721729
# @option options [ Session ] :session The session to use.
730+
# @option options [ Hash | String ] :hint The index to use for this operation.
731+
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
722732
#
723733
# @return [ Result ] The response from the database.
724734
#
@@ -745,6 +755,8 @@ def update_one(filter, update, options = {})
745755
# Defaults to the collection's write concern.
746756
# @option options [ Hash ] :collation The collation to use.
747757
# @option options [ Session ] :session The session to use.
758+
# @option options [ Hash | String ] :hint The index to use for this operation.
759+
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
748760
#
749761
# @return [ BSON::Document, nil ] The document, if found.
750762
#
@@ -781,6 +793,8 @@ def find_one_and_delete(filter, options = {})
781793
# @option options [ Array ] :array_filters A set of filters specifying to which array elements
782794
# an update should apply.
783795
# @option options [ Session ] :session The session to use.
796+
# @option options [ Hash | String ] :hint The index to use for this operation.
797+
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
784798
#
785799
# @return [ BSON::Document ] The document.
786800
#
@@ -815,6 +829,8 @@ def find_one_and_update(filter, update, options = {})
815829
# Defaults to the collection's write concern.
816830
# @option options [ Hash ] :collation The collation to use.
817831
# @option options [ Session ] :session The session to use.
832+
# @option options [ Hash | String ] :hint The index to use for this operation.
833+
# May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. "_id_").
818834
#
819835
# @return [ BSON::Document ] The document.
820836
#

0 commit comments

Comments
 (0)