Skip to content

Commit 98c3b87

Browse files
committed
CDRIVER-2558 transactions documentation
1 parent 567bf77 commit 98c3b87

32 files changed

+336
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ example-gridfs
3737
example-matcher
3838
example-session
3939
example-sdam-monitoring
40+
example-transaction
4041
example-update
4142
filter-bsondump
4243
find-and-modify

src/libmongoc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ mongoc_add_example (example-command-with-opts TRUE ${PROJECT_SOURCE_DIR}/example
803803
mongoc_add_example (example-create-indexes TRUE ${PROJECT_SOURCE_DIR}/examples/example-create-indexes.c)
804804
mongoc_add_example (example-scram TRUE ${PROJECT_SOURCE_DIR}/examples/example-scram.c)
805805
mongoc_add_example (example-session TRUE ${PROJECT_SOURCE_DIR}/examples/example-session.c)
806+
mongoc_add_example (example-transaction TRUE ${PROJECT_SOURCE_DIR}/examples/example-transaction.c)
806807
mongoc_add_example (mongoc-dump TRUE ${PROJECT_SOURCE_DIR}/examples/mongoc-dump.c)
807808
mongoc_add_example (mongoc-ping TRUE ${PROJECT_SOURCE_DIR}/examples/mongoc-ping.c)
808809
mongoc_add_example (mongoc-tail TRUE ${PROJECT_SOURCE_DIR}/examples/mongoc-tail.c)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Read preferences, read and write concern, and collation can be overridden by various sources. The highest-priority sources for these options are listed first:
2+
3+
================== ============== ============== =========
4+
Read Preferences Read Concern Write Concern Collation
5+
================== ============== ============== =========
6+
``read_prefs`` ``opts`` ``opts`` ``opts``
7+
Transaction Transaction Transaction
8+
|opts-source|
9+
================== ============== ============== =========
10+
11+
:ref:`See the example for transactions <mongoc_client_session_start_transaction_example>` and for :ref:`the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Use this function for commands that read such as "count" or "distinct".
2+
3+
.. include:: includes/read-opts-sources.txt
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Read preferences, read concern, and collation can be overridden by various sources. The highest-priority sources for these options are listed first in the following table. No write concern is applied.
2+
3+
================== ============== =========
4+
Read Preferences Read Concern Collation
5+
================== ============== =========
6+
``read_prefs`` ``opts`` ``opts``
7+
Transaction Transaction
8+
|opts-source|
9+
================== ============== =========
10+
11+
:ref:`See the example for transactions <mongoc_client_session_start_transaction_example>` and for :ref:`the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
``opts`` may be NULL or a BSON document with additional command options:
22

33
* ``readConcern``: Construct a :symbol:`mongoc_read_concern_t` and use :symbol:`mongoc_read_concern_append` to add the read concern to ``opts``. See the example code for :symbol:`mongoc_client_read_command_with_opts`. Read concern requires MongoDB 3.2 or later, otherwise an error is returned.
4-
* ``sessionId``: Construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session` and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
4+
* ``sessionId``: First, construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session`. You can begin a transaction with :symbol:`mongoc_client_session_start_transaction`, optionally with a :symbol:`mongoc_transaction_opt_t` that overrides the options inherited from |opts-source|, and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
55
* ``collation``: Configure textual comparisons. See :ref:`Setting Collation Order <setting_collation_order>`, and `the MongoDB Manual entry on Collation <https://docs.mongodb.com/manual/reference/collation/>`_. Collation requires MongoDB 3.2 or later, otherwise an error is returned.
66
* ``serverId``: To target a specific server, include an int32 "serverId" field. Obtain the id by calling :symbol:`mongoc_client_select_server`, then :symbol:`mongoc_server_description_id` on its return value.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Use this function for commands that both read and write, such as "mapReduce" with an output collection.
2+
3+
Read and write concern and collation can be overridden by various sources. The highest-priority sources for these options are listed first in the following table. Read preferences are *not* applied. The write concern is omitted for MongoDB before 3.4.
4+
5+
============== ============== =========
6+
Read Concern Write Concern Collation
7+
============== ============== =========
8+
``opts`` ``opts`` ``opts``
9+
Transaction Transaction
10+
|opts-source| |opts-source|
11+
============== ============== =========
12+
13+
:ref:`See the example for transactions <mongoc_client_session_start_transaction_example>` and for :ref:`the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`.

src/libmongoc/doc/includes/read-write-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
* ``readConcern``: Construct a :symbol:`mongoc_read_concern_t` and use :symbol:`mongoc_read_concern_append` to add the read concern to ``opts``. See the example code for :symbol:`mongoc_client_read_command_with_opts`. Read concern requires MongoDB 3.2 or later, otherwise an error is returned.
44
* ``writeConcern``: Construct a :symbol:`mongoc_write_concern_t` and use :symbol:`mongoc_write_concern_append` to add the write concern to ``opts``. See the example code for :symbol:`mongoc_client_write_command_with_opts`.
5-
* ``sessionId``: Construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session` and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
5+
* ``sessionId``: First, construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session`. You can begin a transaction with :symbol:`mongoc_client_session_start_transaction`, optionally with a :symbol:`mongoc_transaction_opt_t` that overrides the options inherited from |opts-source|, and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
66
* ``collation``: Configure textual comparisons. See :ref:`Setting Collation Order <setting_collation_order>`, and `the MongoDB Manual entry on Collation <https://docs.mongodb.com/manual/reference/collation/>`_. Collation requires MongoDB 3.2 or later, otherwise an error is returned.
77
* ``serverId``: To target a specific server, include an int32 "serverId" field. Obtain the id by calling :symbol:`mongoc_client_select_server`, then :symbol:`mongoc_server_description_id` on its return value.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Use this function for commands that write such as "drop" or "createRole" (but not for "insert", "update", or "delete", see `Basic Write Operations`_). Write concern and collation can be overridden by various sources. The highest-priority sources for these options are listed first in the following table. The write concern is omitted for MongoDB before 3.4.
2+
3+
============== =========
4+
Write Concern Collation
5+
============== =========
6+
``opts`` ``opts``
7+
Transaction
8+
|opts-source|
9+
============== =========
10+
11+
:ref:`See the example for transactions <mongoc_client_session_start_transaction_example>` and for :ref:`the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
``opts`` may be NULL or a BSON document with additional command options:
22

33
* ``writeConcern``: Construct a :symbol:`mongoc_write_concern_t` and use :symbol:`mongoc_write_concern_append` to add the write concern to ``opts``. See the example code for :symbol:`mongoc_client_write_command_with_opts`.
4-
* ``sessionId``: Construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session` and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
4+
* ``sessionId``: First, construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session`. You can begin a transaction with :symbol:`mongoc_client_session_start_transaction`, optionally with a :symbol:`mongoc_transaction_opt_t` that overrides the options inherited from |opts-source|, and use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
55
* ``collation``: Configure textual comparisons. See :ref:`Setting Collation Order <setting_collation_order>`, and `the MongoDB Manual entry on Collation <https://docs.mongodb.com/manual/reference/collation/>`_. Collation requires MongoDB 3.2 or later, otherwise an error is returned.
66
* ``serverId``: To target a specific server, include an int32 "serverId" field. Obtain the id by calling :symbol:`mongoc_client_select_server`, then :symbol:`mongoc_server_description_id` on its return value.

src/libmongoc/doc/mongoc_client_command_with_opts.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ Synopsis
2020
2121
Execute a command on the server, interpreting ``opts`` according to the MongoDB server version. To send a raw command to the server without any of this logic, use :symbol:`mongoc_client_command_simple`.
2222

23-
Read preferences are applied from ``read_prefs`` or else from ``client``. Collation, read concern, and write concern are applied from ``opts`` (:ref:`see example for the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`). The write concern is ignored for MongoDB before 3.4.
23+
.. |opts-source| replace:: ``client``
24+
25+
.. include:: includes/opts-sources.txt
2426

2527
``reply`` is always initialized, and must be freed with :symbol:`bson:bson_destroy()`.
2628

src/libmongoc/doc/mongoc_client_read_command_with_opts.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Synopsis
1919
2020
Execute a command on the server, applying logic that is specific to commands that read, and taking the MongoDB server version into account. To send a raw command to the server without any of this logic, use :symbol:`mongoc_client_command_simple`.
2121

22-
Use this function for commands that read such as "count" or "distinct". Read concern is applied from ``opts`` or else from ``client``. Collation is applied from ``opts`` (see example below). Read preferences are applied from ``read_prefs`` or else from ``client``. No write concern is applied.
22+
.. |opts-source| replace:: ``client``
23+
24+
.. include:: includes/read-cmd-opts-sources.txt
2325

2426
``reply`` is always initialized, and must be freed with :symbol:`bson:bson_destroy()`.
2527

src/libmongoc/doc/mongoc_client_read_write_command_with_opts.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Synopsis
2020
2121
Execute a command on the server, applying logic for commands that both read and write, and taking the MongoDB server version into account. To send a raw command to the server without any of this logic, use :symbol:`mongoc_client_command_simple`.
2222

23-
Use this function for commands that both read and write, such as "mapReduce" with an output collection.
23+
.. |opts-source| replace:: ``client``
2424

25-
Read concern is applied from ``opts`` or else from ``client``. Collation is applied from ``opts`` (:ref:`see example for the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`). Read preferences are *not* applied. Write concern is applied from ``opts``, or else from ``client``. The write concern is omitted for MongoDB before 3.4.
25+
.. include:: includes/read-write-opts-sources.txt
2626

2727
``reply`` is always initialized, and must be freed with :symbol:`bson:bson_destroy()`.
2828

src/libmongoc/doc/mongoc_client_session_start_transaction.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ Return
3030

3131
Returns true if the transaction was started. Returns ``false`` and sets ``error`` if there are invalid arguments, such as a session with a transaction already in progress.
3232

33+
.. _mongoc_client_session_start_transaction_example:
34+
35+
Example
36+
-------
37+
38+
.. literalinclude:: ../examples/example-transaction.c
39+
:language: c
40+
:caption: example-transaction.c
41+
3342
.. only:: html
3443

3544
.. taglist:: See Also:

src/libmongoc/doc/mongoc_client_write_command_with_opts.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ Synopsis
1818
1919
Execute a command on the server, applying logic that is specific to commands that write, and taking the MongoDB server version into account. To send a raw command to the server without any of this logic, use :symbol:`mongoc_client_command_simple`.
2020

21-
Use this function for commands that write such as "drop" or "createRole" (but not for "insert", "update", or "delete", see `Basic Write Operations`_). Write concern is applied from ``opts``, or else from ``client``. The write concern is omitted for MongoDB before 3.4. Collation is applied from ``opts`` (:ref:`see example for the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`). No read concern or read preferences are applied.
21+
.. |opts-source| replace:: ``client``
22+
23+
.. include:: includes/write-opts-sources.txt
2224

2325
``reply`` is always initialized, and must be freed with :symbol:`bson:bson_destroy()`.
2426

src/libmongoc/doc/mongoc_collection_aggregate.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Parameters
2121

2222
* ``collection``: A :symbol:`mongoc_collection_t`.
2323
* ``flags``: A :symbol:`mongoc_query_flags_t`.
24-
* ``pipeline``: A :symbol:`bson:bson_t` containing the pipeline array.
24+
* ``pipeline``: A :symbol:`bson:bson_t`, either a BSON array or a BSON document containing an array field named "pipeline".
2525
* ``opts``: A :symbol:`bson:bson_t` containing options for the command, or ``NULL``.
2626
* ``read_prefs``: A :symbol:`mongoc_read_prefs_t` or ``NULL``.
2727

@@ -40,15 +40,19 @@ For a list of all options, see `the MongoDB Manual entry on the aggregate comman
4040
Description
4141
-----------
4242

43-
This function shall execute an aggregation query on the underlying 'collection'. The bson 'pipeline' is not validated, simply passed along as appropriate to the server. As such, compatibility and errors should be validated in the appropriate server documentation.
43+
This function shall execute an aggregation query on the underlying collection. For more information on building aggregation pipelines, see `the MongoDB Manual entry on the aggregate command <http://docs.mongodb.org/manual/reference/command/aggregate/>`_.
4444

45-
For more information on building MongoDB pipelines, see `the MongoDB Manual entry on the aggregate command <http://docs.mongodb.org/manual/reference/command/aggregate/>`_.
45+
Read preferences, read and write concern, and collation can be overridden by various sources. The highest-priority sources for these options are listed first in the following table. Write concern is applied from ``opts``, or if ``opts`` has no write concern and the aggregation pipeline includes "$out", the write concern is applied from ``collection``. The write concern is omitted for MongoDB before 3.4.
4646

47-
.. note::
47+
================== ============== ============== =========
48+
Read Preferences Read Concern Write Concern Collation
49+
================== ============== ============== =========
50+
``read_prefs`` ``opts`` ``opts`` ``opts``
51+
Transaction Transaction Transaction
52+
``collection`` ``collection`` ``collection``
53+
================== ============== ============== =========
4854

49-
The ``pipeline`` parameter should contain a field named ``pipeline`` containing a BSON array of pipeline stages.
50-
51-
Read concern is applied from ``opts`` or else from ``collection``. Collation is applied from ``opts`` (:ref:`see example for the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`). Read preferences are applied from ``read_prefs`` or else from ``collection``. Write concern is applied from ``opts``, or if ``opts`` has no write concern and the aggregation pipeline includes "$out", the write concern is applied from ``collection``. The write concern is omitted for MongoDB before 3.4.
55+
:ref:`See the example for transactions <mongoc_client_session_start_transaction_example>` and for :ref:`the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`.
5256

5357
Returns
5458
-------

src/libmongoc/doc/mongoc_collection_command_with_opts.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Synopsis
1919
2020
Execute a command on the server, interpreting ``opts`` according to the MongoDB server version. To send a raw command to the server without any of this logic, use :symbol:`mongoc_client_command_simple`.
2121

22-
Read preferences are applied from ``read_prefs`` or else from ``collection``. Collation, read concern, and write concern are applied from ``opts`` (:ref:`see example for the "distinct" command with opts <mongoc_client_read_command_with_opts_example>`). The write concern is ignored for MongoDB before 3.4.
22+
.. |opts-source| replace:: ``collection``
23+
24+
.. include:: includes/opts-sources.txt
2325

2426
``reply`` is always initialized, and must be freed with :symbol:`bson:bson_destroy()`.
2527

src/libmongoc/doc/mongoc_collection_count_with_opts.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Parameters
3030
* ``read_prefs``: An optional :symbol:`mongoc_read_prefs_t`, otherwise uses the collection's read preference.
3131
* ``error``: An optional location for a :symbol:`bson_error_t <errors>` or ``NULL``.
3232

33+
.. |opts-source| replace:: ``collection``
34+
3335
.. include:: includes/read-opts.txt
3436

3537
Description

src/libmongoc/doc/mongoc_collection_drop_index_with_opts.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Parameters
2121
* ``index_name``: A string containing the name of the index.
2222
* ``error``: An optional location for a :symbol:`bson_error_t <errors>` or ``NULL``.
2323

24+
.. |opts-source| replace:: ``collection``
25+
2426
.. include:: includes/write-opts.txt
2527

2628
Description

src/libmongoc/doc/mongoc_collection_drop_with_opts.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Parameters
1919
* ``collection``: A :symbol:`mongoc_collection_t`.
2020
* ``error``: An optional location for a :symbol:`bson_error_t <errors>` or ``NULL``.
2121

22+
.. |opts-source| replace:: ``collection``
23+
2224
.. include:: includes/write-opts.txt
2325

2426
Description

src/libmongoc/doc/mongoc_collection_find_with_opts.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Query on ``collection``, passing arbitrary query options to the server in ``opts
3030

3131
To target a specific server, include an integer "serverId" field in ``opts`` with an id obtained first by calling :symbol:`mongoc_client_select_server`, then :symbol:`mongoc_server_description_id` on its return value.
3232

33+
.. |opts-source| replace:: ``collection``
34+
35+
.. include:: includes/read-opts-sources.txt
36+
3337
Returns
3438
-------
3539

@@ -117,18 +121,22 @@ Option BSON type Option BSON type
117121
``limit`` non-negative int64 ``min`` document
118122
``batchSize`` non-negative int64 ``noCursorTimeout`` bool
119123
``exhaust`` bool ``oplogReplay`` bool
120-
``hint`` string or document ``returnKey`` bool
121-
``allowPartialResults`` bool ``showRecordId`` bool
122-
``awaitData`` bool ``singleBatch`` bool
123-
``collation`` document ``tailable`` bool
124-
``comment`` string
124+
``hint`` string or document ``readConcern`` document
125+
``allowPartialResults`` bool ``returnKey`` bool
126+
``awaitData`` bool ``sessionId`` (none)
127+
``collation`` document ``showRecordId`` bool
128+
``comment`` string ``singleBatch`` bool
125129
======================= ================== =================== ==================
126130

127-
All options are documented in the reference page for `the "find" command`_ in the MongoDB server manual, except for "maxAwaitTimeMS".
131+
All options are documented in the reference page for `the "find" command`_ in the MongoDB server manual, except for "maxAwaitTimeMS" and "sessionId".
128132

129133
"maxAwaitTimeMS" is the maximum amount of time for the server to wait on new documents to satisfy a query, if "tailable" and "awaitData" are both true.
130134
If no new documents are found, the tailable cursor receives an empty batch. The "maxAwaitTimeMS" option is ignored for MongoDB older than 3.4.
131135

136+
To add a "sessionId", construct a :symbol:`mongoc_client_session_t` with :symbol:`mongoc_client_start_session`. You can begin a transaction with :symbol:`mongoc_client_session_start_transaction`, optionally with a :symbol:`mongoc_transaction_opt_t` that overrides the options inherited from ``collection``. Then use :symbol:`mongoc_client_session_append` to add the session to ``opts``. See the example code for :symbol:`mongoc_client_session_t`.
137+
138+
To add a "readConcern", construct a :symbol:`mongoc_read_concern_t` with :symbol:`mongoc_read_concern_new` and configure it with :symbol:`mongoc_read_concern_set_level`. Then use :symbol:`mongoc_read_concern_append` to add the read concern to ``opts``.
139+
132140
For some options like "collation", the driver returns an error if the server version is too old to support the feature.
133141
Any fields in ``opts`` that are not listed here are passed to the server unmodified.
134142

0 commit comments

Comments
 (0)