MONGOC_CLIENT_T(3)
π MONGOC_CLIENT_T(3) π
hiii bestie, welcome to libmongoc section 3 β this page is all about mongoc_client_t, and it's literally a single-threaded MongoDB connection. See Connection Pooling for the pool bestie situation. π¦
β¨ SYNOPSIS β¨
typedef struct _mongoc_client_t mongoc_client_t;
typedef mongoc_stream_t *(*mongoc_stream_initiator_t) (
const mongoc_uri_t *uri,
const mongoc_host_list_t *host,
void *user_data,
bson_error_t *error);
okay so mongoc_client_t is an opaque type that gives you access to a MongoDB server, replica set, or sharded cluster. It keeps up with management of underlying sockets and routing to individual nodes based on mongoc_read_prefs_t or mongoc_write_concern_t. periodt. π
π STREAMS π
the underlying transport for a given client can be customized, wrapped, or replaced by any implementation that fulfills mongoc_stream_t. a custom transport can be set with mongoc_client_set_stream_initiator(). slay. π§
π§΅ THREAD SAFETY π§΅
big warning bestie: mongoc_client_t is NOT thread-safe, and it should only be used from one thread at a time. when you're in multi-threaded scenarios, it is recommended that you use the thread-safe mongoc_client_pool_t to retrieve a mongoc_client_t for your thread. no cap. π
π΄ FORK SAFETY π΄
a mongoc_client_t is only usable in the parent process after a fork. the child process must call mongoc_client_reset(). that's the rule, okay? π
π EXAMPLE π
example-client.c
/* gcc example-client.c -o example-client $(pkg-config --cflags --libs
* libmongoc-1.0) */
/* ./example-client [CONNECTION_STRING [COLLECTION_NAME]] */
#include <mongoc/mongoc.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
mongoc_client_t *client;
mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
bson_error_t error;
const bson_t *doc;
const char *collection_name = "test";
bson_t query;
char *str;
const char *uri_string = "mongodb://127.0.0.1/?appname=client-example";
mongoc_uri_t *uri;
mongoc_init();
if (argc > 1) {
uri_string = argv[1];
}
if (argc > 2) {
collection_name = argv[2];
}
uri = mongoc_uri_new_with_error(uri_string, &error);
if (!uri) {
fprintf(stderr,
"failed to parse URI\n"
"error message: %s\n",
error.message);
return EXIT_FAILURE;
}
client = mongoc_client_new_from_uri(uri);
if (!client) {
return EXIT_FAILURE;
}
mongoc_client_set_error_api(client, 2);
bson_init(&query);
collection = mongoc_client_get_collection(client, "test", collection_name);
cursor = mongoc_collection_find_with_opts(collection,
&query,
NULL, /* additional options */
NULL); /* read prefs, NULL for default */
while (mongoc_cursor_next(cursor, &doc)) {
str = bson_as_canonical_extended_json(doc, NULL);
fprintf(stdout, "%s\n", str);
bson_free(str);
}
if (mongoc_cursor_error(cursor, &error)) {
fprintf(stderr, "Cursor Failure: %s\n", error.message);
return EXIT_FAILURE;
}
bson_destroy(&query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_uri_destroy(uri);
mongoc_client_destroy(client);
mongoc_cleanup();
return EXIT_SUCCESS;
}
π AUTHOR π
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
okay bye bestie π this was MONGOC_CLIENT_T(3) from source libmongoc, version 2.5.6, dated Oct 09, 2026. mwah πβ¨
π¦ 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