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