-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-51321: Configure CRUD operations #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,4 +8,333 @@ Configure CRUD Operations | |||||||||||||||||||||||||||||||||
:local: | ||||||||||||||||||||||||||||||||||
:backlinks: none | ||||||||||||||||||||||||||||||||||
:depth: 2 | ||||||||||||||||||||||||||||||||||
:class: singlecol | ||||||||||||||||||||||||||||||||||
:class: singlecol | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. facet:: | ||||||||||||||||||||||||||||||||||
:name: genre | ||||||||||||||||||||||||||||||||||
:values: reference | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. meta:: | ||||||||||||||||||||||||||||||||||
:keywords: customize, preferences, replica set, consistency, kotlin | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Overview | ||||||||||||||||||||||||||||||||||
-------- | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
In this guide, you can learn how to configure **write concern**, **read concern**, | ||||||||||||||||||||||||||||||||||
and **read preference** options to modify the way that the {+driver-short+} runs | ||||||||||||||||||||||||||||||||||
read and write operations on replica sets. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Read and Write Settings Precedence | ||||||||||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
You can set write concern, read concern, and read preference options at the following | ||||||||||||||||||||||||||||||||||
levels: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
- Client, which sets the *default for all operation executions* unless overridden | ||||||||||||||||||||||||||||||||||
- Transaction | ||||||||||||||||||||||||||||||||||
- Database | ||||||||||||||||||||||||||||||||||
- Collection | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
This list also indicates the increasing order of precedence of the option settings. For | ||||||||||||||||||||||||||||||||||
example, if you set a read concern for a transaction, it will override the read | ||||||||||||||||||||||||||||||||||
concern settings inherited from the client. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Write concern, read concern, and read preference options allow you to customize the | ||||||||||||||||||||||||||||||||||
causal consistency and availability of the data in your replica sets. To see a full | ||||||||||||||||||||||||||||||||||
list of these options, see the following guides in the {+mdb-server+} manual: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
- :manual:`Read Preference </core/read-preference/>` | ||||||||||||||||||||||||||||||||||
- :manual:`Read Concern </reference/read-concern/>` | ||||||||||||||||||||||||||||||||||
- :manual:`Write Concern </reference/write-concern/>` | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. _kotlin-sync-read-write-config: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Configure Read and Write Operations | ||||||||||||||||||||||||||||||||||
----------------------------------- | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
You can control how the driver routes read operations among replica set members | ||||||||||||||||||||||||||||||||||
by setting a read preference. You can also control how the driver waits for | ||||||||||||||||||||||||||||||||||
acknowledgment of read and write operations on a replica set by setting read and | ||||||||||||||||||||||||||||||||||
write concerns. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
The following sections show how to configure these read and write settings | ||||||||||||||||||||||||||||||||||
at various levels. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. _kotlin-sync-read-write-client: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Client Configuration | ||||||||||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
This example shows how to set the read preference, read concern, and | ||||||||||||||||||||||||||||||||||
write concern of a ``MongoClient`` instance by passing a ``MongoClientSettings`` | ||||||||||||||||||||||||||||||||||
instance to the ``MongoClient.create()`` method. The code configures the | ||||||||||||||||||||||||||||||||||
following settings: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
- ``secondary`` read preference: Read operations retrieve data from | ||||||||||||||||||||||||||||||||||
secondary replica set members. | ||||||||||||||||||||||||||||||||||
- ``LOCAL`` read concern: Read operations return the instance's most recent data | ||||||||||||||||||||||||||||||||||
without guaranteeing that the data has been written to a majority of the replica | ||||||||||||||||||||||||||||||||||
set members. | ||||||||||||||||||||||||||||||||||
- ``W2`` write concern: The primary replica set member and one secondary member | ||||||||||||||||||||||||||||||||||
must acknowledge the write operation. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. literalinclude:: /includes/configure-crud.kt | ||||||||||||||||||||||||||||||||||
:language: kotlin | ||||||||||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||||||||||
:start-after: start-client-settings | ||||||||||||||||||||||||||||||||||
:end-before: end-client-settings | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Alternatively, you can specify the read and write settings in the connection | ||||||||||||||||||||||||||||||||||
URI, which is passed as a parameter to the ``MongoClient.create()`` method: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. literalinclude:: /includes/configure-crud.kt | ||||||||||||||||||||||||||||||||||
:language: kotlin | ||||||||||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||||||||||
:start-after: start-client-settings-uri | ||||||||||||||||||||||||||||||||||
:end-before: end-client-settings-uri | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. _kotlin-sync-read-write-transaction: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Transaction Configuration | ||||||||||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
This example shows how to set the read preference, read concern, and | ||||||||||||||||||||||||||||||||||
write concern of a transaction by passing a ``TransactionOptions`` | ||||||||||||||||||||||||||||||||||
instance to the ``withTransaction()`` method. Transactions run within | ||||||||||||||||||||||||||||||||||
**sessions**, which are groupings of related read or write operations that you | ||||||||||||||||||||||||||||||||||
intend to run sequentially. Before applying the transaction options, use the | ||||||||||||||||||||||||||||||||||
``startSession()`` method to start a session. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. tip:: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
To learn more about sessions, see :manual:`Server Sessions </reference/server-sessions/>` | ||||||||||||||||||||||||||||||||||
in the {+mdb-server+} manual. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
The example configures the following settings: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
- ``primary`` read preference: Read operations retrieve data from | ||||||||||||||||||||||||||||||||||
the primary replica set member. | ||||||||||||||||||||||||||||||||||
- ``MAJORITY`` read concern: Read operations return the instance's most recent data | ||||||||||||||||||||||||||||||||||
that has been written to a majority of replica set members. | ||||||||||||||||||||||||||||||||||
- ``W1`` write concern: The primary replica set member must acknowledge the | ||||||||||||||||||||||||||||||||||
write operation. | ||||||||||||||||||||||||||||||||||
Comment on lines
+115
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No periods needed
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. literalinclude:: /includes/configure-crud.kt | ||||||||||||||||||||||||||||||||||
:language: kotlin | ||||||||||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||||||||||
:start-after: start-transaction-settings | ||||||||||||||||||||||||||||||||||
:end-before: end-transaction-settings | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. _kotlin-sync-read-write-database: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Database Configuration | ||||||||||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
This example shows how to set the read preference, read concern, and | ||||||||||||||||||||||||||||||||||
write concern of a database called ``test_database`` by chaining setter | ||||||||||||||||||||||||||||||||||
methods to the ``getDatabase()`` method. The code configures the following | ||||||||||||||||||||||||||||||||||
settings: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
- ``primaryPreferred`` read preference: Read operations retrieve data from | ||||||||||||||||||||||||||||||||||
the primary replica set member, or secondary members if the primary is unavailable. | ||||||||||||||||||||||||||||||||||
- ``AVAILABLE`` read concern: Read operations return the instance's most recent data | ||||||||||||||||||||||||||||||||||
without guaranteeing that the data has been written to a majority of the replica | ||||||||||||||||||||||||||||||||||
set members. | ||||||||||||||||||||||||||||||||||
- ``MAJORITY`` write concern: The majority of all replica set members | ||||||||||||||||||||||||||||||||||
must acknowledge the write operation. | ||||||||||||||||||||||||||||||||||
Comment on lines
+138
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No periods needed
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. literalinclude:: /includes/configure-crud.kt | ||||||||||||||||||||||||||||||||||
:language: kotlin | ||||||||||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||||||||||
:start-after: start-database-settings | ||||||||||||||||||||||||||||||||||
:end-before: end-database-settings | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. _kotlin-sync-read-write-collection: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Collection Configuration | ||||||||||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
This example shows how to set the read preference, read concern, and | ||||||||||||||||||||||||||||||||||
write concern of a collection called ``test_collection`` by chaining setter | ||||||||||||||||||||||||||||||||||
methods to the ``getCollection()`` method. The code configures the following | ||||||||||||||||||||||||||||||||||
settings: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
- ``secondaryPreferred`` read preference: Read operations retrieve data from | ||||||||||||||||||||||||||||||||||
secondary replica set members, or the primary members if no secondary members are | ||||||||||||||||||||||||||||||||||
available. | ||||||||||||||||||||||||||||||||||
- ``AVAILABLE`` read concern: Read operations return the instance's most recent data | ||||||||||||||||||||||||||||||||||
without guaranteeing that the data has been written to a majority of the replica | ||||||||||||||||||||||||||||||||||
set members. | ||||||||||||||||||||||||||||||||||
- ``UNACKNOWLEDGED`` write concern: Replica set members do not need to acknowledge | ||||||||||||||||||||||||||||||||||
the write operation. | ||||||||||||||||||||||||||||||||||
Comment on lines
+162
to
+169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. literalinclude:: /includes/configure-crud.kt | ||||||||||||||||||||||||||||||||||
:language: kotlin | ||||||||||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||||||||||
:start-after: start-collection-settings | ||||||||||||||||||||||||||||||||||
:end-before: end-collection-settings | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. _kotlin-sync-read-write-advanced: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Advanced Read Configurations | ||||||||||||||||||||||||||||||||||
---------------------------- | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
The following sections describe ways to further customize how the {+driver-short+} | ||||||||||||||||||||||||||||||||||
routes read operations. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. _kotlin-sync-sharded-clusters: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Sharded Clusters | ||||||||||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
You can specify a read preference when connecting to a sharded cluster. | ||||||||||||||||||||||||||||||||||
MongoDB uses sharding to divide datasets by key ranges and distribute data across multiple | ||||||||||||||||||||||||||||||||||
database instances. A sharded cluster, or the set of nodes in a sharded deployment, | ||||||||||||||||||||||||||||||||||
includes the following components: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
- **Shard**: A replica set that contains a subset of the sharded data. | ||||||||||||||||||||||||||||||||||
- **Mongos**: A query router that provides an interface between your | ||||||||||||||||||||||||||||||||||
application and the sharded cluster. | ||||||||||||||||||||||||||||||||||
- **Config servers**: Servers that store the cluster's configuration settings | ||||||||||||||||||||||||||||||||||
and metadata. | ||||||||||||||||||||||||||||||||||
Comment on lines
+195
to
+199
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
.. tip:: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
To learn more about sharded clusters, see :manual:`Sharding </sharding/>` | ||||||||||||||||||||||||||||||||||
in the {+mdb-server+} manual. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
When reading from the replica set shards, mongos applies your specified read | ||||||||||||||||||||||||||||||||||
|
When reading from the replica set shards, mongos applies your specified read | |
When reading from the replica set shards, ``mongos`` applies your specified read |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When connecting to a sharded cluster, the {+driver-short+} determines the closest mongos | |
When connecting to a sharded cluster, the {+driver-short+} determines the closest ``mongos`` |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
determines the latency window by adding this mongos's average round-trip time to the | |
determines the latency window by adding this ``mongos``'s average round-trip time to the |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
across up to two random mongos instances that fall within the latency window. For each request, | |
across up to two random ``mongos`` instances that fall within the latency window. For each request, |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, the driver uses only mongos instances or replica set members whose | |
By default, the driver uses only ``mongos`` instances or replica set members whose |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When selecting replica set members from a single mongos instance, the | |
When selecting replica set members from a single ``mongos`` instance, the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No periods needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think since these are full sentences, they should have periods - https://www.mongodb.com/docs/meta/style-guide/style/lists/#list-items
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call