Docs Menu
Docs Home
/ / /
C Driver

Read Data from MongoDB

On this page

  • Overview
  • Sample Application
  • Find Documents
  • Count Documents in a Collection
  • Count Documents Returned from a Query
  • Estimated Document Count
  • Retrieve Distinct Values
  • Monitor Data Changes

On this page, you can see copyable code examples that show common functions you can use to retrieve documents by using the C driver.

Tip

To learn more about any of the functions shown on this page, see the link to a relevant guide provided in each section.

To use an example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders in the code examples, such as <connection string URI>, with the relevant values for your MongoDB deployment.

You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:

  1. Ensure you have the C driver installed.

  2. Copy the following code and paste it into a new .c file.

  3. Copy a code example from this page and paste it on the specified lines in the file.

1#include <bson/bson.h>
2#include <mongoc/mongoc.h>
3#include <stdio.h>
4
5int
6main (void)
7{
8 mongoc_client_t *client;
9 mongoc_collection_t *collection;
10 char *str;
11 bson_error_t error;
12
13 mongoc_init ();
14
15 client = mongoc_client_new ("<connection string URI>");
16 collection = mongoc_client_get_collection (client, "<database name>", "collection name");
17
18 // Start example code here
19
20 // End example code here
21
22 mongoc_collection_destroy (collection);
23 mongoc_client_destroy (client);
24 mongoc_cleanup ();
25
26 return EXIT_SUCCESS;
27}

Tip

For instructions about how to install the C driver, see Download and Install in the Get Started guide.

The following example retrieves a list of documents that match the criteria specified by the given filter:

bson_t *query = bson_new ();
// Add fields to query here
mongoc_cursor_t* results = mongoc_collection_find_with_opts (collection, query, NULL, NULL);
const bson_t *doc;
while (mongoc_cursor_next (results, &doc)) {
str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
mongoc_cursor_destroy (results);
bson_destroy (query);

To learn more about the mongoc_collection_find_with_opts() function, see the Find Documents guide.

The following example returns the number of documents in the specified collection:

bson_t *query = bson_new ();
int64_t count =
mongoc_collection_count_documents (collection, query, NULL, NULL, NULL, &error);
printf ("%" PRId64 "\n", count);
bson_destroy (query);

To learn more about the mongoc_collection_count_documents() function, see the Retrieve an Accurate Count section of the Count Documents guide.

The following example returns the number of documents that match the criteria specified by the given filter:

bson_t *query = bson_new ();
// Add fields to query here
int64_t count =
mongoc_collection_count_documents (collection, query, NULL, NULL, NULL, &error);
printf ("%" PRId64 "\n", count);
bson_destroy (query);

To learn more about the mongoc_collection_count_documents() function, see the Retrieve an Accurate Count section of the Count Documents guide.

The following example returns an approximate number of documents in the specified collection based on collection metadata:

int64_t count =
mongoc_collection_estimated_document_count (collection, NULL, NULL, NULL, &error);
printf ("%" PRId64 "\n", count);

To learn more about the mongoc_collection_estimated_document_count() function, see the Retrieve an Estimated Count section of the Count Documents guide.

The following example returns all distinct values of the specified field name in a given collection:

bson_t reply;
bson_t *command = BCON_NEW ("distinct",
BCON_UTF8 ("<collection name>"),
"key",
BCON_UTF8 ("<field name>"));
if (!mongoc_collection_read_command_with_opts (collection, command, NULL, NULL, &reply, &error)) {
fprintf (stderr, "An error occurred: %s\n", error.message);
} else {
str = bson_as_canonical_extended_json (&reply, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy(&reply);
bson_destroy(command);

To learn more about the distinct command, see the Retrieve Distinct Field Values guide.

The following example creates a change stream for a given collection and prints out subsequent change events in that collection:

mongoc_change_stream_t *change_stream;
bson_t *pipeline = bson_new ();
// Add stages to pipeline here
const bson_t *doc;
change_stream = mongoc_collection_watch (collection, pipeline, NULL);
while (mongoc_change_stream_next (change_stream, &doc)) {
str = bson_as_canonical_extended_json (doc, NULL);
printf ("Change: %s\n", str);
bson_free (str);
}
bson_destroy (pipeline);
mongoc_change_stream_destroy (change_stream);

To learn more about the mongoc_collection_watch() function, see the Monitor Data Changes guide.

Back

Store Large Files