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 β€” opts first, then Transaction, then database
  • Collation β€” opts first

(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 β€” A mongoc_database_t. your database, your main character.
  • db_name β€” The name of the database to run the command on.
  • command β€” A bson_t containing the command specification. the actual thing you're asking for ✨
  • opts β€” A bson_t containing additional options.
  • reply β€” A maybe-NULL pointer to overwritable storage for a bson_t to contain the results.
  • error β€” An optional location for a bson_error_t or NULL.

opts may be NULL or a BSON document with additional command options:

  • writeConcern β€” Construct a mongoc_write_concern_t and use mongoc_write_concern_append() to add the write concern to opts. See the example code for mongoc_client_write_command_with_opts().
  • sessionId β€” First, construct a mongoc_client_session_t with mongoc_client_start_session(). You can begin a transaction with mongoc_client_session_start_transaction(), optionally with a mongoc_transaction_opt_t that overrides the options inherited from database, and use mongoc_client_session_append() to add the session to opts. See the example code for mongoc_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 calling mongoc_client_select_server(), then mongoc_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

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