1MONGOC_CURSOR_T(3)                 libmongoc                MONGOC_CURSOR_T(3)
2
3
4
5Client-side cursor abstraction
6

SYNOPSIS

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

THREAD SAFETY

30       mongoc_cursor_t is NOT thread safe. It may only be used from within the
31       thread in which it was created.
32

EXAMPLE

34       Query MongoDB and iterate results
35
36          /* gcc example-client.c -o example-client $(pkg-config --cflags --libs
37           * libmongoc-1.0) */
38
39          /* ./example-client [CONNECTION_STRING [COLLECTION_NAME]] */
40
41          #include <mongoc/mongoc.h>
42          #include <stdio.h>
43          #include <stdlib.h>
44
45          int
46          main (int argc, char *argv[])
47          {
48             mongoc_client_t *client;
49             mongoc_collection_t *collection;
50             mongoc_cursor_t *cursor;
51             bson_error_t error;
52             const bson_t *doc;
53             const char *collection_name = "test";
54             bson_t query;
55             char *str;
56             const char *uri_string = "mongodb://127.0.0.1/?appname=client-example";
57             mongoc_uri_t *uri;
58
59             mongoc_init ();
60             if (argc > 1) {
61                uri_string = argv[1];
62             }
63
64             if (argc > 2) {
65                collection_name = argv[2];
66             }
67
68             uri = mongoc_uri_new_with_error (uri_string, &error);
69             if (!uri) {
70                fprintf (stderr,
71                         "failed to parse URI: %s\n"
72                         "error message:       %s\n",
73                         uri_string,
74                         error.message);
75                return EXIT_FAILURE;
76             }
77
78             client = mongoc_client_new_from_uri (uri);
79             if (!client) {
80                return EXIT_FAILURE;
81             }
82
83             mongoc_client_set_error_api (client, 2);
84
85             bson_init (&query);
86             collection = mongoc_client_get_collection (client, "test", collection_name);
87             cursor = mongoc_collection_find_with_opts (
88                collection,
89                &query,
90                NULL,  /* additional options */
91                NULL); /* read prefs, NULL for default */
92
93             while (mongoc_cursor_next (cursor, &doc)) {
94                str = bson_as_canonical_extended_json (doc, NULL);
95                fprintf (stdout, "%s\n", str);
96                bson_free (str);
97             }
98
99             if (mongoc_cursor_error (cursor, &error)) {
100                fprintf (stderr, "Cursor Failure: %s\n", error.message);
101                return EXIT_FAILURE;
102             }
103
104             bson_destroy (&query);
105             mongoc_cursor_destroy (cursor);
106             mongoc_collection_destroy (collection);
107             mongoc_uri_destroy (uri);
108             mongoc_client_destroy (client);
109             mongoc_cleanup ();
110
111             return EXIT_SUCCESS;
112          }
113
114

AUTHOR

116       MongoDB, Inc
117
119       2017-present, MongoDB, Inc
120
121
122
123
1241.25.1                           Nov 08, 2023               MONGOC_CURSOR_T(3)
Impressum