Skip to content

Commit 12b697c

Browse files
authored
CDRIVER-4198 let option for CRUD commands (#957)
* Add "let" option to CRUD methods * mongoc_bulk_operation_set_let * Support "let" option for delete, update, and findAndModify * CRUD spec tests for "let" option Synced with mongodb/specifications@1028c34 Also includes some test changes related to CDRIVER-4063 * Document "let" option for mongoc_collection_find_with_opts * mongoc_bulk_operation_t tests for "let" option
1 parent ef80008 commit 12b697c

37 files changed

+3363
-130
lines changed

build/generate-opts.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ def __init__(self, items, **defaults):
151151
'help': 'A document or string that specifies the index to use to support the query predicate.'
152152
})
153153

154+
let_option = ('let', {
155+
'type': 'document',
156+
'help': 'A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.'
157+
})
158+
154159
opts_structs = OrderedDict([
155160
('mongoc_crud_opts_t', Shared([
156161
write_concern_option,
@@ -164,6 +169,7 @@ def __init__(self, items, **defaults):
164169
collation_option,
165170
hint_option,
166171
upsert_option,
172+
let_option,
167173
])),
168174

169175
('mongoc_insert_one_opts_t', Struct([
@@ -181,6 +187,7 @@ def __init__(self, items, **defaults):
181187
('crud', {'type': 'mongoc_crud_opts_t'}),
182188
collation_option,
183189
hint_option,
190+
let_option,
184191
])),
185192

186193
('mongoc_delete_one_opts_t', Struct([
@@ -209,6 +216,7 @@ def __init__(self, items, **defaults):
209216
write_concern_option,
210217
ordered_option,
211218
session_option,
219+
let_option,
212220
], allow_extra=False, ordered='true')),
213221

214222
('mongoc_bulk_insert_opts_t', Struct([
@@ -321,13 +329,14 @@ def __init__(self, items, **defaults):
321329
collation_option,
322330
server_option,
323331
('batchSize', {'type': 'int32_t', 'help': 'An ``int32`` representing number of documents requested to be returned on each call to :symbol:`mongoc_cursor_next`', 'check_set': True}),
324-
('let', {'type': 'document', 'help': 'A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language'})
332+
let_option,
325333
])),
326334

327335
('mongoc_find_and_modify_appended_opts_t', Struct([
328336
write_concern_option,
329337
session_option,
330-
hint_option
338+
hint_option,
339+
let_option,
331340
], opts_name='extra'))
332341
])
333342

src/libmongoc/doc/includes/aggregate-opts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
* ``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.
1313
* ``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.
1414
* ``batchSize``: An ``int32`` representing number of documents requested to be returned on each call to :symbol:`mongoc_cursor_next`
15-
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language
15+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.

src/libmongoc/doc/includes/bulk-opts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
* ``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`.
99
* ``ordered``: set to ``false`` to attempt to insert all documents, continuing after errors.
1010
* ``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`.
11+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.

src/libmongoc/doc/includes/delete-many-opts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
1111
* ``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.
1212
* ``hint``: A document or string that specifies the index to use to support the query predicate.
13+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.

src/libmongoc/doc/includes/delete-one-opts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
* ``validate``: Construct a bitwise-or of all desired :symbol:`bson_validate_flags_t <bson_validate_with_error>`. Set to ``false`` to skip client-side validation of the provided BSON documents.
1111
* ``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.
1212
* ``hint``: A document or string that specifies the index to use to support the query predicate.
13+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.

src/libmongoc/doc/includes/find-and-modify-appended-opts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
* ``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`.
99
* ``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`.
1010
* ``hint``: A document or string that specifies the index to use to support the query predicate.
11+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.

src/libmongoc/doc/includes/replace-one-opts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
* ``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.
1313
* ``hint``: A document or string that specifies the index to use to support the query predicate.
1414
* ``upsert``: When true, creates a new document if no document matches the query.
15+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.

src/libmongoc/doc/includes/update-many-opts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
* ``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.
1313
* ``hint``: A document or string that specifies the index to use to support the query predicate.
1414
* ``upsert``: When true, creates a new document if no document matches the query.
15+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.
1516
* ``arrayFilters``: An array of filters specifying to which array elements an update should apply.

src/libmongoc/doc/includes/update-one-opts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
* ``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.
1313
* ``hint``: A document or string that specifies the index to use to support the query predicate.
1414
* ``upsert``: When true, creates a new document if no document matches the query.
15+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.
1516
* ``arrayFilters``: An array of filters specifying to which array elements an update should apply.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
:man_page: mongoc_bulk_operation_set_let
2+
3+
mongoc_bulk_operation_set_let()
4+
===============================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
mongoc_bulk_operation_set_let (
13+
mongoc_bulk_operation_t *bulk, const bson_t *let);
14+
15+
Parameters
16+
----------
17+
18+
* ``bulk``: A :symbol:`mongoc_bulk_operation_t`.
19+
* ``let``: A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.
20+
21+
Description
22+
-----------
23+
24+
Defines constants that can be accessed by all update, replace, and delete operations executed as part of this :doc:`bulk <mongoc_bulk_operation_t>`.
25+
26+
It is prohibited to call this function after adding operations to the :doc:`bulk <mongoc_bulk_operation_t>`.

src/libmongoc/doc/mongoc_bulk_operation_t.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ After adding all of the write operations to the ``mongoc_bulk_operation_t``, cal
5050
mongoc_bulk_operation_set_bypass_document_validation
5151
mongoc_bulk_operation_set_client_session
5252
mongoc_bulk_operation_set_hint
53+
mongoc_bulk_operation_set_let
5354
mongoc_bulk_operation_update
5455
mongoc_bulk_operation_update_many_with_opts
5556
mongoc_bulk_operation_update_one

src/libmongoc/doc/mongoc_collection_find_with_opts.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Option BSON type Option BSON type
129129
``awaitData`` bool ``sessionId`` (none)
130130
``collation`` document ``showRecordId`` bool
131131
``comment`` string ``singleBatch`` bool
132-
``allowDiskUse`` bool
132+
``allowDiskUse`` bool ``let`` document
133133
======================= ================== =================== ==================
134134

135135
All options are documented in the reference page for `the "find" command`_ in the MongoDB server manual, except for "maxAwaitTimeMS", "sessionId", and "exhaust".

src/libmongoc/src/mongoc/mongoc-bulk-operation-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct _mongoc_bulk_operation_t {
3333
mongoc_client_session_t *session;
3434
mongoc_write_concern_t *write_concern;
3535
mongoc_bulk_write_flags_t flags;
36+
bson_t let;
3637
uint32_t server_id;
3738
mongoc_array_t commands;
3839
mongoc_write_result_t result;

src/libmongoc/src/mongoc/mongoc-bulk-operation.c

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ mongoc_bulk_operation_new (bool ordered)
5454
bulk->flags.ordered = ordered;
5555
bulk->server_id = 0;
5656

57+
bson_init (&bulk->let);
58+
5759
_mongoc_array_init (&bulk->commands, sizeof (mongoc_write_command_t));
5860
_mongoc_write_result_init (&bulk->result);
5961

@@ -102,6 +104,7 @@ mongoc_bulk_operation_destroy (mongoc_bulk_operation_t *bulk) /* IN */
102104

103105
bson_free (bulk->database);
104106
bson_free (bulk->collection);
107+
bson_destroy (&bulk->let);
105108
mongoc_write_concern_destroy (bulk->write_concern);
106109
_mongoc_array_destroy (&bulk->commands);
107110

@@ -135,7 +138,7 @@ mongoc_bulk_operation_destroy (mongoc_bulk_operation_t *bulk) /* IN */
135138
} while (0)
136139

137140

138-
bool
141+
static bool
139142
_mongoc_bulk_operation_remove_with_opts (
140143
mongoc_bulk_operation_t *bulk,
141144
const bson_t *selector,
@@ -145,6 +148,7 @@ _mongoc_bulk_operation_remove_with_opts (
145148
{
146149
mongoc_write_command_t command = {0};
147150
mongoc_write_command_t *last;
151+
bson_t cmd_opts = BSON_INITIALIZER;
148152
bson_t opts;
149153
bool has_collation;
150154
bool ret = false;
@@ -193,8 +197,12 @@ _mongoc_bulk_operation_remove_with_opts (
193197
}
194198
}
195199

200+
if (!bson_empty (&bulk->let)) {
201+
bson_append_document (&cmd_opts, "let", 3, &bulk->let);
202+
}
203+
196204
_mongoc_write_command_init_delete (
197-
&command, selector, NULL, &opts, bulk->flags, bulk->operation_id);
205+
&command, selector, &cmd_opts, &opts, bulk->flags, bulk->operation_id);
198206

199207
command.flags.has_collation = has_collation;
200208
command.flags.has_delete_hint = has_delete_hint;
@@ -204,6 +212,7 @@ _mongoc_bulk_operation_remove_with_opts (
204212
ret = true;
205213

206214
done:
215+
bson_destroy (&cmd_opts);
207216
bson_destroy (&opts);
208217
RETURN (ret);
209218
}
@@ -408,6 +417,7 @@ _mongoc_bulk_operation_update_append (
408417
{
409418
mongoc_write_command_t command = {0};
410419
mongoc_write_command_t *last;
420+
bson_t cmd_opts = BSON_INITIALIZER;
411421
bson_t opts;
412422
bool has_collation;
413423
bool has_array_filters;
@@ -445,20 +455,31 @@ _mongoc_bulk_operation_update_append (
445455
last->flags.has_update_hint |= has_update_hint;
446456
last->flags.has_multi_write |= update_opts->multi;
447457
_mongoc_write_command_update_append (last, selector, document, &opts);
448-
bson_destroy (&opts);
449-
return;
458+
GOTO (done);
450459
}
451460
}
452461

453-
_mongoc_write_command_init_update (
454-
&command, selector, document, &opts, bulk->flags, bulk->operation_id);
462+
if (!bson_empty (&bulk->let)) {
463+
bson_append_document (&cmd_opts, "let", 3, &bulk->let);
464+
}
465+
466+
_mongoc_write_command_init_update (&command,
467+
selector,
468+
document,
469+
&cmd_opts,
470+
&opts,
471+
bulk->flags,
472+
bulk->operation_id);
455473

456474
command.flags.has_array_filters = has_array_filters;
457475
command.flags.has_collation = has_collation;
458476
command.flags.has_update_hint = has_update_hint;
459477
command.flags.has_multi_write = update_opts->multi;
460478

461479
_mongoc_array_append_val (&bulk->commands, command);
480+
481+
done:
482+
bson_destroy (&cmd_opts);
462483
bson_destroy (&opts);
463484
}
464485

@@ -950,3 +971,20 @@ mongoc_bulk_operation_set_bypass_document_validation (
950971

951972
bulk->flags.bypass_document_validation = bypass;
952973
}
974+
975+
976+
void
977+
mongoc_bulk_operation_set_let (mongoc_bulk_operation_t *bulk, const bson_t *let)
978+
{
979+
BSON_ASSERT (bulk);
980+
BSON_ASSERT (let);
981+
982+
/* This method cannot be called after appending operations, as the CRUD spec
983+
* states the option should apply to all commands (excluding insert). Since
984+
* commands are initialized as operations are added, allowing "let" to be
985+
* changed at any time could violate that contract. */
986+
BSON_ASSERT (bulk->commands.len == 0);
987+
988+
bson_destroy (&bulk->let);
989+
bson_copy_to (let, &bulk->let);
990+
}

src/libmongoc/src/mongoc/mongoc-bulk-operation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ mongoc_bulk_operation_update_one_with_opts (mongoc_bulk_operation_t *bulk,
115115
MONGOC_EXPORT (void)
116116
mongoc_bulk_operation_set_bypass_document_validation (
117117
mongoc_bulk_operation_t *bulk, bool bypass);
118+
MONGOC_EXPORT (void)
119+
mongoc_bulk_operation_set_let (
120+
mongoc_bulk_operation_t *bulk, const bson_t *let);
118121

119122

120123
/*

0 commit comments

Comments
 (0)