MONGOC_COLLEC...MAND_WITH_OPTS(3)

✨ MONGOC_COLLEC...MAND_WITH_OPTS(3) ✨

okay so this is a libmongoc page, bestie β€” the mongoc_collection_command_with_opts one β€” and it's genuinely iconic once you get past the ✨formality✨. let's go πŸ’…

✨ SYNOPSIS ✨

bool
mongoc_collection_command_with_opts (
   mongoc_collection_t *collection,
   const bson_t *command,
   const mongoc_read_prefs_t *read_prefs,
   const bson_t *opts,
   bson_t *reply,
   bson_error_t *error);

So like β€” this bad boy runs a command on the server, and it interprets opts according to the MongoDB server version πŸ¦‹. That's the whole vibe. But listen: if you wanna send a raw command straight to the server with none of this logic, none of this thinking-for-you stuff, you go use mongoc_client_command_simple() instead. Different tool, different energy, iykyk πŸ’€


πŸŽ€ the priority tea (who overrides who) πŸŽ€

Read preferences, read and write concern, and collation can all be overridden by various sources, and ngl the ordering matters. The highest-priority sources for these options are listed first:

Read Preferences (first wins):

  • read_prefs
  • Transaction

Read Concern (first wins):

  • opts
  • Transaction

Write Concern (first wins):

  • opts
  • Transaction

Collation (first wins):

  • opts
🚨 okay this part is important, don't skip it: in a transaction, read concern and write concern are prohibited in opts, and the read preference must be primary or NULL. Periodt.

See the example for transactions and for the "distinct" command with opts. πŸ“–

Also β€” little heads up 🫢 β€” reply is always initialized, and you must free it with bson_destroy().

✨ PARAMETERS ✨

  • collection β€” A mongoc_collection_t. πŸ’–
  • command β€” A bson_t containing the command specification.
  • read_prefs β€” An optional mongoc_read_prefs_t.
  • opts β€” A bson_t containing additional options.
  • reply β€” A maybe-NULL pointer to overwritable storage for a bson_t to contain the results. (maybe-NULL! it's giving flexible πŸ‘‘)
  • error β€” An optional location for a bson_error_t or NULL.

🌸 what even goes in opts tho 🌸

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 collection, 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. No cap, that's where the per-command deets live. πŸ“š

πŸ’€ real talk: this function is not considered a retryable read operation. so don't come for it expecting retry magic, it's not that girl.

✨ ERRORS ✨

Errors are propagated via the error parameter. Simple as that, bestie. 🫢

✨ RETURNS ✨

Returns true if successful. Returns false and sets error if there are invalid arguments or a server or network error. 😭

Also note: the reply is not parsed for a write concern timeout or write concern error. so yeah, don't go digging in there for that. πŸ™ˆ

✨ EXAMPLE ✨

See the example code for mongoc_client_read_command_with_opts(). 🌈

✨ AUTHOR ✨

MongoDB, Inc πŸ‘‘

2009-present, MongoDB, Inc.


and that's the whole page, bestie!! πŸŽ€ you understood it, you ate, you slayed, no crumbs left. πŸ’…

this has been libmongoc version 2.5.6, dated Oct 09, 2026 β€” mwah, go run some commands πŸ’–βœ¨

πŸ“¦ where this page comes from