1MONGOC_CLIENT_T(3)             MongoDB C Driver             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
21       underlying  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.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
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
90          #if 0
91             bson_append_utf8 (&query, "hello", -1, "world", -1);
92          #endif
93
94             collection = mongoc_client_get_collection (client, "test", collection_name);
95             cursor = mongoc_collection_find_with_opts (
96                collection,
97                &query,
98                NULL,  /* additional options */
99                NULL); /* read prefs, NULL for default */
100
101             while (mongoc_cursor_next (cursor, &doc)) {
102                str = bson_as_canonical_extended_json (doc, NULL);
103                fprintf (stdout, "%s\n", str);
104                bson_free (str);
105             }
106
107             if (mongoc_cursor_error (cursor, &error)) {
108                fprintf (stderr, "Cursor Failure: %s\n", error.message);
109                return EXIT_FAILURE;
110             }
111
112             bson_destroy (&query);
113             mongoc_cursor_destroy (cursor);
114             mongoc_collection_destroy (collection);
115             mongoc_uri_destroy (uri);
116             mongoc_client_destroy (client);
117             mongoc_cleanup ();
118
119             return EXIT_SUCCESS;
120          }
121
122

AUTHOR

124       MongoDB, Inc
125
127       2017-present, MongoDB, Inc
128
129
130
131
1321.13.1                           Jan 24, 2019               MONGOC_CLIENT_T(3)
Impressum