Docs Menu
Docs Home
/ / /
C Driver

Optimize Queries by Using Indexes

On this page

  • Overview
  • Sample Application
  • Single Field Index
  • Compound Index
  • Multikey Index
  • Geospatial Index
  • Unique Index
  • Wildcard Index
  • Clustered Index
  • Text Index
  • Delete an Index
  • Atlas Search Index Management
  • Create Search Index
  • List Search Indexes
  • Update Search Indexes
  • Delete Search Indexes

On this page, you can see copyable code examples that show how to manage different types of indexes by using the C driver.

Tip

To learn more about working with indexes, see the Work with Indexes guide. To learn more about any of the indexes shown on this page, see the link 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 bson_error_t error;
11
12 mongoc_init ();
13
14 client = mongoc_client_new ("<connection string URI>");
15 collection = mongoc_client_get_collection (client, "<database name>", "collection name");
16
17 // Start example code here
18
19 // End example code here
20
21 mongoc_collection_destroy (collection);
22 mongoc_client_destroy (client);
23 mongoc_cleanup ();
24
25 return EXIT_SUCCESS;
26}

The following example creates an ascending index on the specified field:

bson_t *keys = BCON_NEW ("<field name>", BCON_INT32 (1));
mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL);
if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) {
printf ("Successfully created index\n");
} else {
fprintf (stderr, "Failed to create index: %s", error.message);
}
bson_destroy (keys);
mongoc_index_model_destroy (index_model);

To learn more about single field indexes, see the Single-Field Indexes guide.

The following example creates a compound index of two ascending indexes on the specified fields:

bson_t *keys = BCON_NEW ("<field name 1>", BCON_INT32 (1), "<field name 2>", BCON_INT32 (1));
mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL);
if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) {
printf ("Successfully created index\n");
} else {
fprintf (stderr, "Failed to create index: %s", error.message);
}
bson_destroy (keys);
mongoc_index_model_destroy (index_model);

To learn more about compound indexes, see the Compound Indexes guide.

The following example creates an ascending multikey index on the specified array-valued field:

bson_t *keys = BCON_NEW ("<array field name>", BCON_INT32 (1));
mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL);
if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) {
printf ("Successfully created index\n");
} else {
fprintf (stderr, "Failed to create index: %s", error.message);
}
bson_destroy (keys);
mongoc_index_model_destroy (index_model);

To learn more about multikey indexes, see the Multikey Indexes guide.

The following example creates a 2dsphere index on the specified field that contains GeoJSON objects:

bson_t *keys = BCON_NEW ("<GeoJSON object field name>", BCON_UTF8 ("2dsphere"));
mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL);
if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) {
printf ("Successfully created index\n");
} else {
fprintf (stderr, "Failed to create index: %s", error.message);
}
bson_destroy (keys);
mongoc_index_model_destroy (index_model);

To learn more about the GeoJSON data type, see GeoJSON Objects in the MongoDB Server manual.

The following example creates an ascending unique index on the specified field:

bson_t *keys = BCON_NEW ("title", BCON_INT32 (1));
bson_t *opts = BCON_NEW ("unique", BCON_BOOL (true));
mongoc_index_model_t *index_model = mongoc_index_model_new (keys, opts);
if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) {
printf ("Successfully created index\n");
} else {
fprintf (stderr, "Failed to create index: %s", error.message);
}
bson_destroy (keys);
bson_destroy (opts);
mongoc_index_model_destroy (index_model);

The following example creates an ascending wildcard index in the specified collection:

bson_t *keys = BCON_NEW ("$**", BCON_INT32 (1));
mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL);
if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) {
printf ("Successfully created index\n");
} else {
fprintf (stderr, "Failed to create index: %s", error.message);
}
bson_destroy (keys);
mongoc_index_model_destroy (index_model);

The following example creates a new collection with an ascending clustered index on the _id field:

bson_t *opts = BCON_NEW ("clusteredIndex", "{",
"key", "{",
"_id", BCON_INT32 (1),
"}",
"unique", BCON_BOOL (true),
"}");
mongoc_database_t *database = mongoc_client_get_database (client, "<database name>");
if (mongoc_database_create_collection (database, "<collection name>", opts, &error)) {
printf ("Successfully created collection\n");
} else {
fprintf (stderr, "Failed to create collection: %s", error.message);
}
mongoc_database_destroy (database);
bson_destroy (opts);

The following example creates a text index on the specified string field:

bson_t *keys = BCON_NEW ("<field name>", BCON_UTF8 ("text"));
mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL);
if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) {
printf ("Successfully created index\n");
} else {
fprintf (stderr, "Failed to create index: %s", error.message);
}
bson_destroy (keys);
mongoc_index_model_destroy (index_model);

The following example deletes an index with the specified name:

if (mongoc_collection_drop_index (collection, "<index name>", &error)) {
printf ("Successfully dropped index\n");
} else {
fprintf (stderr, "Failed to drop index: %s", error.message);
}

To learn more about removing indexes, see Remove an Index in the Work with Indexes guide.

The following sections contain code examples that describe how to manage Atlas Search indexes.

To learn more about Atlas Search indexes, see the Atlas Search Indexes guide.

The following example creates an Atlas Search index on the specified field:

bson_t cmd;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"createSearchIndexes" : "%s",
"indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<index name>"} ]
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully created search index\n");
} else {
fprintf (stderr, "Failed to create search index: %s", error.message);
}
bson_destroy (&cmd);

To learn more about creating search indexes, see the Create a Search Index guide.

The following example prints a list of Atlas Search indexes in the specified collection:

bson_t pipeline;
const bson_t *doc;
const char *pipeline_str = BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
bson_init_from_json (&pipeline, pipeline_str, -1, &error);
mongoc_cursor_t *cursor =
mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL);
while (mongoc_cursor_next (cursor, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (&pipeline);
mongoc_cursor_destroy (cursor);

To learn more about listing search indexes, see the List Search Indexes guide.

The following example updates an existing Atlas Search index with the specified new index definition:

bson_t cmd;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"updateSearchIndex" : "%s",
"definition" : {"mappings" : {"dynamic" : true}}, "name" : "<index name>"}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully updated search index\n");
} else {
fprintf (stderr, "Failed to create search index: %s", error.message);
}
bson_destroy (&cmd);

To learn more about updating search indexes, see the Update a Search Index guide.

The following example deletes an Atlas Search index with the specified name:

bson_t cmd;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"dropSearchIndexes" : "%s",
"index" : "<index name>"
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully deleted search index\n");
} else {
fprintf (stderr, "Failed to delete search index: %s", error.message);
}
bson_destroy (&cmd);

To learn more about deleting search indexes, see the Delete a Search Index guide.

Back

Monitor Changes