1MONGOC_CLIENT_T(3) libmongoc MONGOC_CLIENT_T(3)
2
3
4
5A single-threaded MongoDB connection. See Connection Pooling.
6
8 typedef struct _mongoc_client_t mongoc_client_t;
9
10 typedef mongoc_stream_t *(*mongoc_stream_initiator_t) (
11 const mongoc_uri_t *uri,
12 const mongoc_host_list_t *host,
13 void *user_data,
14 bson_error_t *error);
15
16 mongoc_client_t is an opaque type that provides access to a MongoDB
17 server, replica set, or sharded cluster. It maintains management of un‐
18 derlying sockets and routing to individual nodes based on
19 mongoc_read_prefs_t or mongoc_write_concern_t.
20
22 The underlying transport for a given client can be customized, wrapped
23 or replaced by any implementation that fulfills mongoc_stream_t. A cus‐
24 tom transport can be set with mongoc_client_set_stream_initiator().
25
27 mongoc_client_t is NOT thread-safe and should only be used from one
28 thread at a time. When used in multi-threaded scenarios, it is recom‐
29 mended that you use the thread-safe mongoc_client_pool_t to retrieve a
30 mongoc_client_t for your thread.
31
33 A mongoc_client_t is only usable in the parent process after a fork.
34 The child process must call mongoc_client_reset().
35
37 example-client.c
38
39 /* gcc example-client.c -o example-client $(pkg-config --cflags --libs
40 * libmongoc-1.0) */
41
42 /* ./example-client [CONNECTION_STRING [COLLECTION_NAME]] */
43
44 #include <mongoc/mongoc.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47
48 int
49 main (int argc, char *argv[])
50 {
51 mongoc_client_t *client;
52 mongoc_collection_t *collection;
53 mongoc_cursor_t *cursor;
54 bson_error_t error;
55 const bson_t *doc;
56 const char *collection_name = "test";
57 bson_t query;
58 char *str;
59 const char *uri_string = "mongodb://127.0.0.1/?appname=client-example";
60 mongoc_uri_t *uri;
61
62 mongoc_init ();
63 if (argc > 1) {
64 uri_string = argv[1];
65 }
66
67 if (argc > 2) {
68 collection_name = argv[2];
69 }
70
71 uri = mongoc_uri_new_with_error (uri_string, &error);
72 if (!uri) {
73 fprintf (stderr,
74 "failed to parse URI: %s\n"
75 "error message: %s\n",
76 uri_string,
77 error.message);
78 return EXIT_FAILURE;
79 }
80
81 client = mongoc_client_new_from_uri (uri);
82 if (!client) {
83 return EXIT_FAILURE;
84 }
85
86 mongoc_client_set_error_api (client, 2);
87
88 bson_init (&query);
89 collection = mongoc_client_get_collection (client, "test", collection_name);
90 cursor = mongoc_collection_find_with_opts (
91 collection,
92 &query,
93 NULL, /* additional options */
94 NULL); /* read prefs, NULL for default */
95
96 while (mongoc_cursor_next (cursor, &doc)) {
97 str = bson_as_canonical_extended_json (doc, NULL);
98 fprintf (stdout, "%s\n", str);
99 bson_free (str);
100 }
101
102 if (mongoc_cursor_error (cursor, &error)) {
103 fprintf (stderr, "Cursor Failure: %s\n", error.message);
104 return EXIT_FAILURE;
105 }
106
107 bson_destroy (&query);
108 mongoc_cursor_destroy (cursor);
109 mongoc_collection_destroy (collection);
110 mongoc_uri_destroy (uri);
111 mongoc_client_destroy (client);
112 mongoc_cleanup ();
113
114 return EXIT_SUCCESS;
115 }
116
117
119 MongoDB, Inc
120
122 2017-present, MongoDB, Inc
123
124
125
126
1271.25.1 Nov 08, 2023 MONGOC_CLIENT_T(3)