Docs Menu
Docs Home
/ / /
C Driver
/

Retrieve Data

On this page

  • Overview
  • Sample Data
  • Find Documents
  • Find Documents Example
  • Modify Find Behavior
  • Additional Information
  • API Documentation

In this guide, you can learn how to use the C driver to retrieve data from a MongoDB collection by using read operations. You can call the mongoc_collection_find_with_opts() function to retrieve documents that match a set of criteria specified in a query filter.

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

The mongoc_collection_find_with_opts() function takes a query filter and returns all matching documents from a collection. A query filter is a document that specifies the criteria that the driver uses to match documents from the collection.

To learn more about query filters, see the Specify a Query guide.

The following example uses the mongoc_collection_find_with_opts() function to find all documents in which the value of the cuisine field is "Spanish":

bson_t *filter = BCON_NEW ("cuisine", BCON_UTF8 ("Spanish"));
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, NULL, NULL);

The mongoc_collection_find_with_opts() operation in the preceding example returns a mongoc_cursor_t *, which you can iterate through by using the mongoc_cursor_next() function and a while loop. The following example iterates through and prints the results returned in the previous query:

const bson_t *doc;
bson_error_t error;
while (mongoc_cursor_next (results, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
// Ensure we iterated through cursor without error
if (mongoc_cursor_error (results, &error)) {
fprintf (stderr, "Error getting results: %s\n", error.message);
}
mongoc_cursor_destroy (results);
bson_destroy (filter);
{ "_id" : { "$oid" : "..." }, "name" : "Charle'S Corner Restaurant & Deli", "cuisine" : "Spanish", ... }
{ "_id" : { "$oid" : "..." }, "name" : "Real Madrid Restaurant", "cuisine" : "Spanish", ... }
{ "_id" : { "$oid" : "..." }, "name" : "Malaga Restaurant", "cuisine" : "Spanish", ... }
{ "_id" : { "$oid" : "..." }, "name" : "Cafe Espanol", "cuisine" : "Spanish", ... }
{ "_id" : { "$oid" : "..." }, "name" : "Cafe Riazor", "cuisine" : "Spanish", ... }
...

Note

Find All Documents

To find all documents in a collection, pass an empty filter to the mongoc_collection_find_with_opts() function:

bson_t *empty_filter = bson_new ();
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, empty_filter, NULL, NULL);
mongoc_cursor_destroy (results);
bson_destroy (empty_filter);

You can modify the behavior of the mongoc_collection_find_with_opts() function by by passing in a bson_t structure that contains the options you want to configure. The following table describes options commonly used for modifying queries:

Option
Description
collation
Sets the collation options for the query.
comment
Specifies a string to attach to the query. This can help you trace and interpret the operation in the server logs and in profile data. To learn more about query comments, see $comment in the MongoDB Server manual.
hint
Specifies the index to use for the query.
limit
Limits the number of documents to be returned from the query.
maxTimeMS
Sets the maximum execution time on the server for this operation.
skip
Sets the number of documents to skip.
sort
Defines the sort criteria to apply to the query.

The following example uses the limit and maxTimeMS options to limit the number of documents returned by the query to 10 and set a maximum execution time of 10000 milliseconds on the operation:

bson_t *filter = BCON_NEW ("cuisine", BCON_UTF8 ("Spanish"));
bson_t *opts = BCON_NEW ("limit", BCON_INT32 (10), "maxTimeMS", BCON_INT32 (10000));
mongoc_cursor_t *results =
mongoc_collection_find_with_opts (collection, filter, opts, NULL);
mongoc_cursor_destroy (results);
bson_destroy (filter);
bson_destroy (opts);

For a full list of options that modify the behavior of mongoc_collection_find_with_opts(), see the find method documentation in the MongoDB Server manual.

To learn more about query filters, see Specify a Query.

To view runnable code examples that retrieve documents by using the C driver, see Read Data from MongoDB.

To learn more about any of the functions discussed in this guide, see the following API documentation:

Back

Specify a Query