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 โ€” A mongoc_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 โ€” A bson_t containing the command specification. this one actually matters bestie. ๐Ÿ”ฅ
  • fields โ€” Unused. decorative.
  • read_prefs โ€” An optional mongoc_read_prefs_t. otherwise, the command uses mode MONGOC_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

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