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 inopts, and the read preference must beprimaryorNULL. 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_tcontaining the command specification. - read_prefs β An optional
mongoc_read_prefs_t. - opts β A
bson_tcontaining additional options. - reply β A maybe-
NULLpointer to overwritable storage for abson_tto contain the results. (maybe-NULL! it's giving flexible π) - error β An optional location for a
bson_error_torNULL.
πΈ what even goes in opts tho πΈ
opts may be NULL, or a BSON document with additional command options:
readConcernβ Construct amongoc_read_concern_tand usemongoc_read_concern_append()to add the read concern toopts. See the example code formongoc_client_read_command_with_opts(). Read concern requires MongoDB 3.2 or later, otherwise an error is returned. β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. 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 π
β¨ COPYRIGHT β¨
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
- 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