1MONGOC_CURSOR_T(3)             MongoDB C Driver             MONGOC_CURSOR_T(3)
2
3
4

NAME

6       mongoc_cursor_t - mongoc_cursor_t
7
8       Client-side cursor abstraction
9

SYNOPSIS

11          typedef struct _mongoc_cursor_t mongoc_cursor_t;
12
13       mongoc_cursor_t provides access to a MongoDB query cursor.  It wraps up
14       the wire protocol negotiation required to initiate a query and retrieve
15       an unknown number of documents.
16
17       Common cursor operations include:
18
19       · Determine   which   host   we've   connected   to   with  mongoc_cur‐
20         sor_get_host().
21
22       · Retrieve more records with repeated calls to mongoc_cursor_next().
23
24       · Clone a query to repeat execution at a later point  with  mongoc_cur‐
25         sor_clone().
26
27       · Test for errors with mongoc_cursor_error().
28
29       Cursors are lazy, meaning that no connection is established and no net‐
30       work traffic occurs until the first call to mongoc_cursor_next().
31

THREAD SAFETY

33       mongoc_cursor_t is NOT thread safe. It may only be used from within the
34       thread in which it was created.
35

EXAMPLE

37       Query MongoDB and iterate results.INDENT 0.0
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
64             if (argc > 1) {
65                uri_string = argv[1];
66             }
67
68             if (argc > 2) {
69                collection_name = argv[2];
70             }
71
72             uri = mongoc_uri_new_with_error (uri_string, &error);
73             if (!uri) {
74                fprintf (stderr,
75                         "failed to parse URI: %s\n"
76                         "error message:       %s\n",
77                         uri_string,
78                         error.message);
79                return EXIT_FAILURE;
80             }
81
82             client = mongoc_client_new_from_uri (uri);
83             if (!client) {
84                return EXIT_FAILURE;
85             }
86
87             mongoc_client_set_error_api (client, 2);
88
89             bson_init (&query);
90
91          #if 0
92             bson_append_utf8 (&query, "hello", -1, "world", -1);
93          #endif
94
95             collection = mongoc_client_get_collection (client, "test", collection_name);
96             cursor = mongoc_collection_find_with_opts (
97                collection,
98                &query,
99                NULL,  /* additional options */
100                NULL); /* read prefs, NULL for default */
101
102             while (mongoc_cursor_next (cursor, &doc)) {
103                str = bson_as_canonical_extended_json (doc, NULL);
104                fprintf (stdout, "%s\n", str);
105                bson_free (str);
106             }
107
108             if (mongoc_cursor_error (cursor, &error)) {
109                fprintf (stderr, "Cursor Failure: %s\n", error.message);
110                return EXIT_FAILURE;
111             }
112
113             bson_destroy (&query);
114             mongoc_cursor_destroy (cursor);
115             mongoc_collection_destroy (collection);
116             mongoc_uri_destroy (uri);
117             mongoc_client_destroy (client);
118             mongoc_cleanup ();
119
120             return EXIT_SUCCESS;
121          }
122
123

AUTHOR

125       MongoDB, Inc
126
128       2017-present, MongoDB, Inc
129
130
131
132
1331.13.1                           Jan 24, 2019               MONGOC_CURSOR_T(3)
Impressum