MONGOC_COLLEC...MAND_WITH_OPTS(3)
π MONGOC_COLLECTION_WRITE_COMMAND_WITH_OPTS π
okay bestie, welcome to libmongoc β yes that's the manual we're living in today, and this page is all about mongoc_collection_write_command_with_opts. Buckle up, it's giving β¨specialisedβ¨.
β¨ SYNOPSIS β¨
bool
mongoc_collection_write_command_with_opts (mongoc_collection_t *collection,
const bson_t *command,
const bson_t *opts,
bson_t *reply,
bson_error_t *error);
π¦ DESCRIPTION π¦
So what does this icon actually do? It executes a command on the server, but with extra logic baked in β logic that is specific to commands that write, and it takes the MongoDB server version into account. Like, it knows things. π
If you wanna send a raw command to the server with none of this logic (no thoughts, head empty), use mongoc_collection_command_simple() instead.
Use this function for commands that write, like "drop" or "createRole" β but not for "insert", "update", or "delete" (those have their own whole thing, see Basic Write Operations below, iykyk).
Write concern and collation can be overridden by various sources. β οΈ Warning: in a transaction, read concern and write concern are prohibited in opts. Not optional, fr fr.
The highest-priority sources for these options are listed first. Here's the tea β:
Write Concern (highest priority first):
opts- Transaction
- collection
Collation (highest priority first):
opts
And a lil detail that matters: the write concern is omitted for MongoDB before 3.4.
See the example for transactions and for the "distinct" command with opts.
Also super important: reply is always initialized, and must be freed with bson_destroy(). Don't forget, bestie, that's a leak waiting to happen π«Ά
πΈ PARAMETERS πΈ
- _collection_ β A
mongoc_collection_t. - _command_ β A
bson_tcontaining the command specification. - _opts_ β A
bson_tcontaining additional options. - _reply_ β A maybe-
NULLpointer to overwritable storage for abson_tto contain the results. - _error_ β An optional location for a
bson_error_torNULL.
π the opts document, explained π
opts may be NULL or a BSON document with additional command options:
writeConcernβ Construct amongoc_write_concern_tand usemongoc_write_concern_append()to add the write concern toopts. See the example code formongoc_client_write_command_with_opts().sessionIdβ First, construct amongoc_client_session_twithmongoc_client_start_session(). You can begin a transaction withmongoc_client_session_start_transaction(), optionally with amongoc_transaction_opt_tthat overrides the options inherited fromcollection, and usemongoc_client_session_append()to add the session toopts. See the example code formongoc_client_session_t.collationβ Configure textual comparisons. See Setting Collation Order, and the MongoDB Manual entry on Collation. Collation requires MongoDB 3.2 or later, otherwise an error is returned. πserverIdβ To target a specific server, include an int32"serverId"field. Obtain the id by callingmongoc_client_select_server(), thenmongoc_server_description_id()on its return value.
Consult the MongoDB Manual entry on Database Commands for each command's arguments. π
π ERRORS π
Errors are propagated via the error parameter. That's the whole vibe. Simple, clean.
β RETURNS β
Returns true if successful. Returns false and sets error if there are invalid arguments or a server or network error.
A write concern timeout or write concern error is considered a failure. π So don't go thinking a timeout is a cute little "maybe", it's a no.
π₯ BASIC WRITE OPERATIONS π₯
Do not use this function to call the basic write commands "insert", "update", and "delete". Those commands require special logic not implemented in mongoc_collection_write_command_with_opts. Like, they're on a whole other level, this function is not invited to that party.
For basic write operations use CRUD functions such as mongoc_collection_insert_one() and the others described in the CRUD tutorial, or use the Bulk API. That's the slay route. π
π EXAMPLE π
See the example code for mongoc_client_read_command_with_opts().
π AUTHOR π
MongoDB, Inc
π§ COPYRIGHT π§
2009-present, MongoDB, Inc.
and that's the whole page, bestie!! πβ¨ this little masterpiece came to you from libmongoc version 2.5.6, dated Oct 09, 2026 β mwah, go forth and write commands responsibly ππ¦π
π¦ where this page comes from
- epel 10: mongo-c-driver-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms