MONGOC_DATABA...MAND_WITH_OPTS(3)
π MONGOC_DATABASE_WRITE_COMMAND_WITH_OPTS(3) π
okay so bestie, welcome to the libmongoc manual β this one's about mongoc_database_write_command_with_opts, and honestly? it's giving "the responsible friend who handles the writing commands so you don't have to". let's get into it π«Ά
β¨ SYNOPSIS β¨
bool
mongoc_database_write_command_with_opts (mongoc_database_t *database,
const bson_t *command,
const bson_t *opts,
bson_t *reply,
bson_error_t *error);
πΈ DESCRIPTION πΈ
So what this does is execute a command on the server, but with all the extra logic that's specific to commands that write, and it takes the MongoDB server version into account while doing it. like, it's paying attention. it's aware. π
If you want to send a raw command to the server with none of that logic β like, zero thoughts, head empty β use mongoc_database_command_simple() instead.
Use this function for commands that write, such as "drop" or "createRole" β but not for "insert", "update", or "delete" (peep Basic Write Operations below for why, bestie, it's important π).
Write concern and collation can be overridden by various sources. And heads up β in a transaction, read concern and write concern are prohibited in opts. periodt, don't try it.
The highest-priority sources for these options are listed first here:
- Write Concern β
optsfirst, then Transaction, thendatabase - Collation β
optsfirst
(and yes, that's the whole priority list β Write Concern has three tiers and Collation only has opts. the write concern is omitted for MongoDB before 3.4 π)
Also: see the example for transactions, and for the "distinct" command with opts. iykyk.
And finally β reply is always initialized, and it must be freed with bson_destroy(). don't forget. i'm watching. π
π PARAMETERS π
databaseβ Amongoc_database_t. your database, your main character.db_nameβ The name of the database to run the command on.commandβ Abson_tcontaining the command specification. the actual thing you're asking for β¨optsβ Abson_tcontaining additional options.replyβ A maybe-NULLpointer to overwritable storage for abson_tto contain the results.errorβ An optional location for abson_error_torNULL.
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 fromdatabase, 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. so like, know your server version bestie π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. that's where the deets live π¦
π¦ ERRORS π¦
Errors are propagated via the error parameter. simple as that, no drama π
π₯ RETURNS π₯
Returns true if successful. Returns false and sets error if there are invalid arguments or a server or network error. π
and listen β a write concern timeout or write concern error is considered a failure. so if you were like "but it technically went through though?" β no. failure. no cap. π
π BASIC WRITE OPERATIONS π
Do not use this function to call the basic write commands "insert", "update", and "delete". Those commands require special logic that is not implemented in mongoc_database_write_command_with_opts. like, it's not built for that, it's not that girl, respect the boundaries please β
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.
β EXAMPLE β
See the example code for mongoc_client_read_command_with_opts(). go look, it's right there, it ate π
π§ AUTHOR π§
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
okay that's the whole page bestie, you're now officially informed π and this was libmongoc 2.5.6, dated Oct 09, 2026 β mwah, slay, go write some 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