1MONGOC_CLIENT_T(3)                 libmongoc                MONGOC_CLIENT_T(3)
2
3
4

NAME

6       mongoc_client_t - mongoc_client_t
7
8       A single-threaded MongoDB connection. See connection-pooling.
9

SYNOPSIS

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 un‐
21       derlying  sockets  and  routing  to  individual  nodes  based  on  mon‐
22       goc_read_prefs_t or mongoc_write_concern_t.
23

STREAMS

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

THREAD SAFETY

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

EXAMPLE

36       example-client.c
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             collection = mongoc_client_get_collection (client, "test", collection_name);
89             cursor = mongoc_collection_find_with_opts (
90                collection,
91                &query,
92                NULL,  /* additional options */
93                NULL); /* read prefs, NULL for default */
94
95             while (mongoc_cursor_next (cursor, &doc)) {
96                str = bson_as_canonical_extended_json (doc, NULL);
97                fprintf (stdout, "%s\n", str);
98                bson_free (str);
99             }
100
101             if (mongoc_cursor_error (cursor, &error)) {
102                fprintf (stderr, "Cursor Failure: %s\n", error.message);
103                return EXIT_FAILURE;
104             }
105
106             bson_destroy (&query);
107             mongoc_cursor_destroy (cursor);
108             mongoc_collection_destroy (collection);
109             mongoc_uri_destroy (uri);
110             mongoc_client_destroy (client);
111             mongoc_cleanup ();
112
113             return EXIT_SUCCESS;
114          }
115
116

AUTHOR

118       MongoDB, Inc
119
121       2017-present, MongoDB, Inc
122
123
124
125
1261.20.0                           Nov 18, 2021               MONGOC_CLIENT_T(3)
Impressum