MONGOC_DATABAS...MAND_WITH_OPTS(3)

πŸ’… MONGOC_DATABAS...MAND_WITH_OPTS(3) πŸ’…

Okay so bestie, welcome to the libmongoc page for MONGOC_DATABAS...MAND_WITH_OPTS(3) β€” yes, the manual name is literally truncated like that, it’s giving mysterious main character energy. We’re learning the read-write command function, and it’s honestly iconic once you get the vibe. πŸ’–


✨ SYNOPSIS ✨

bool
mongoc_database_read_write_command_with_opts (
   mongoc_database_t *database,
   const bson_t *command,
   const mongoc_read_prefs_t *read_prefs /* UNUSED */,
   const bson_t *opts,
   bson_t *reply,
   bson_error_t *error);

πŸ’– DESCRIPTION πŸ’–

So this function executes a command on the server, applying the special logic for commands that both read and write, and it takes the MongoDB server version into account. If you want to send a raw command to the server without any of this logic, use mongoc_database_command_simple() instead. No cap.

Use this function for commands that both read and write, such as "mapReduce" with an output collection. That’s the whole lane.

Read and write concern and collation can be overridden by various sources. In a transaction, read concern and write concern are prohibited in opts.

🚨 Bestie, read this twice: in a transaction, read concern and write concern are prohibited in opts. The function is not gonna let that slide.

The highest-priority sources for these options are listed first. Here’s the priority table energy, translated into list form:

  • Read concern β€” highest priority first: opts, then Transaction, then database.
  • Write concern β€” highest priority first: opts, then Transaction, then database.
  • Collation β€” highest priority first: opts.

Read preferences are not applied. The write concern is omitted for MongoDB before 3.4. Periodt.

See the example for transactions and for the "distinct" command with opts.

reply is always initialized, and must be freed with bson_destroy(). Literally always β€” don’t leave your bson_t hanging.

🫢 Little lore drop: the mongoc_read_prefs_t parameter was included by mistake when this function was introduced in libmongoc 1.5. A command that writes must not obey a read preference. So yeah, that parameter is lowkey just sitting there.

🫢 PARAMETERS 🫢

  • database β€” A mongoc_database_t.
  • command β€” A bson_t containing the command specification.
  • read_prefs β€” Ignored. Not used. It’s giving decorative.
  • 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:

  • readConcern β€” Construct a mongoc_read_concern_t and use mongoc_read_concern_append() to add the read concern to opts. See the example code for mongoc_client_read_command_with_opts(). Read concern requires MongoDB 3.2 or later, otherwise an error is returned.
  • 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.
  • 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 per-command deets live.


😭 ERRORS 😭

Errors are propagated via the error parameter. That’s the whole mood.


πŸ”₯ 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 think you can sneak past that one, bestie.


πŸ“ EXAMPLE πŸ“

See the example code for mongoc_client_read_command_with_opts().


πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc


2009-present, MongoDB, Inc.


πŸ’– That’s the page! Source/version 2.5.6, dated Oct 09, 202 (the footer is literally like that), from libmongoc. Stay hydrated, free your bson_t with bson_destroy(), and slay bestie! πŸŽ€

πŸ“¦ where this page comes from