SYNOPSIS
bool mongoc_cursor_next (mongoc_cursor_t *cursor, const bson_t **bson);
DESCRIPTION
The mongoc_cursor_next() function shall iterate the underlying cursor, setting bson to the current document.
mongoc_cursor_next() is a blocking function.
RETURN VALUE
The mongoc_cursor_next() function returns true if a valid bson document was read from the cursor. If false, the cursor is exhausted.
ERRORS
Errors in the read can be determined with the mongoc_cursor_error(3) function.
LIFECYCLE
The bson objects set in mongoc_cursor_next(3) are ephemeral and good until the next call. This means that you must copy the returned bson if you wish to retain it beyond the lifetime of a single call to mongoc_cursor_next().
EXAMPLE
As an example of how to read all of the documents in a collection
/* gcc example.c -o example $(pkg-config --cflags --libs libmongoc-1.0) */ /* ./example-client [CONNECTION_STRING [COLLECTION_NAME]] */ #include <mongoc.h> #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc; const char *uristr = "mongodb://127.0.0.1/"; const char *collection_name = "test"; bson_t query; char *str; mongoc_init (); if (argc > 1) { uristr = argv [1]; } if (argc > 2) { collection_name = argv [2]; } client = mongoc_client_new (uristr); if (!client) { fprintf (stderr, "Failed to parse URI.\n"); return EXIT_FAILURE; } bson_init (&query); #if 0 bson_append_utf8 (&query, "hello", -1, "world", -1); #endif collection = mongoc_client_get_collection (client, "test", collection_name); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, /* Fields, NULL for all. */ NULL); /* Read Prefs, NULL for default */ while (!mongoc_cursor_error (cursor, &error) && mongoc_cursor_more (cursor)) { if (mongoc_cursor_next (cursor, &doc)) { str = bson_as_json (doc, NULL); fprintf (stdout, "%s\n", str); bson_free (str); } } if (mongoc_cursor_error (cursor, &error)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); return EXIT_FAILURE; } bson_destroy (&query); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_client_destroy (client); return EXIT_SUCCESS; }
SEE ALSO
AUTHORS
This page was written by MongoDB Inc.