MONGOC_CLIENT_COMMAND(3)
๐ mongoc_client_command(3) โ a libmongoc moment
okay bestie, gather round, because we're talking about mongoc_client_command from libmongoc, and there is some tea to spill right at the top. ๐ซ
โ ๏ธ WARNING: Deprecated since version 1.29.0. This function is deprecated and should not be used in new code. Use mongoc_client_command_simple() instead. ๐ซ
likeโฆ she's iconic but she's retired, you know? moving on. ๐
โจ SYNOPSIS โจ
mongoc_cursor_t *
mongoc_client_command (mongoc_client_t *client,
const char *db_name,
mongoc_query_flags_t flags,
uint32_t skip,
uint32_t limit,
uint32_t batch_size,
const bson_t *query,
const bson_t *fields,
const mongoc_read_prefs_t *read_prefs);
and a lil PSA: this function is not considered a retryable read operation. no retries, no do-overs, it's giving one-shot. ๐
๐ฆ DESCRIPTION ๐ฆ
so what does she actually do? she creates a cursor that will execute the command when you call mongoc_cursor_next() on it. lazy girl behavior, honestly, and we respect it. โจ
important vibe check though: the client's read preference, read concern, and write concern are not applied to the command. and mongoc_cursor_next() will not check the server response for a write concern error or write concern timeout. so don't come for her later, she told you upfront. ๐ญ
if mongoc_cursor_next() returns false, then go retrieve your error details with mongoc_cursor_error() or mongoc_cursor_error_document(). that's where the lore lives. ๐
๐ PARAMETERS ๐
clientโ Amongoc_client_t. the main character. ๐db_nameโ The name of the database to run the command on.flagsโ Unused. literally just vibing there. ๐คskipโ Unused. also napping.limitโ Unused. same energy.batch_sizeโ Unused. not her moment.queryโ Abson_tcontaining the command specification. this one actually matters bestie. ๐ฅfieldsโ Unused. decorative.read_prefsโ An optionalmongoc_read_prefs_t. otherwise, the command uses modeMONGOC_READ_PRIMARY. ๐ธ
๐ MIGRATING ๐
so mongoc_client_command() is deprecated. periodt. ๐
here's the glow-up, side by side. first, the old way:
Before
const bson_t *reply;
bson_t *cmd = BCON_NEW ("find", "foo", "filter", "{", "}");
mongoc_cursor_t *cursor = mongoc_client_command (client,
"db",
MONGOC_QUERY_NONE /* unused */,
0 /* unused */,
0 /* unused */,
0 /* unused */,
cmd,
NULL /* unused */,
NULL /* read prefs */);
// Expect cursor to return exactly one document for the command reply.
EXPECT (mongoc_cursor_next (cursor, &reply));
bson_error_t error;
if (mongoc_cursor_error (cursor, &error)) {
FAIL ("Expected no error, got: %s\n", error.message);
}
// Expect successful reply to contain "ok": 1
bson_iter_t iter;
EXPECT (bson_iter_init_find (&iter, reply, "ok") && bson_iter_as_int64 (&iter) == 1);
// Expect cursor to return no other documents.
EXPECT (!mongoc_cursor_next (cursor, &reply));
mongoc_cursor_destroy (cursor);
bson_destroy (cmd);
The above code block may be rewritten to use mongoc_client_command_simple() instead, as shown below:
After
bson_t reply;
bson_error_t error;
bson_t *cmd = BCON_NEW ("find", "foo", "filter", "{", "}");
if (!mongoc_client_command_simple (client, "db", cmd, NULL /* read prefs */, &reply, &error)) {
FAIL ("Expected no error, got: %s\n", error.message);
}
// Expect successful reply to contain "ok": 1
bson_iter_t iter;
EXPECT (bson_iter_init_find (&iter, reply, "ok") && bson_iter_as_int64 (&iter) == 1);
bson_destroy (&reply);
bson_destroy (cmd);
see? cursor-free, chaos-free, slay. ๐งโจ
๐ RETURNS ๐
A mongoc_cursor_t. ๐ซ
and listen: the cursor should be freed with mongoc_cursor_destroy(). don't leave her hanging, that's a memory leak and it's not cute. ๐ฎ
โญ AUTHOR โญ
MongoDB, Inc
๐ COPYRIGHT ๐
2009-present, MongoDB, Inc.
and that's the whole page bestie โ libmongoc, version 1.30.13, dated Oct 09, 2026, MONGOC_CLIENT_COMMAND(3). go forth and use mongoc_client_command_simple() instead, ily, mwah ๐
๐ฆ where this page comes from
- fedora 43: mongo-c-driver-devel 1.30.13-1.fc43 ยท Apache-2.0 AND ISC AND MIT AND Zlib
- fedora 43: mongo-c-driver-devel 1.30.13-1.fc43 ยท Apache-2.0 AND ISC AND MIT AND Zlib
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms