Docs 菜单
Docs 主页

使用索引优化查询

在此页面上,您可以查看可复制的代码示例,这些示例展示了如何使用C驱动程序管理不同类型的索引。

提示

要学习;了解有关使用索引的更多信息,请参阅 索引指南。要学习;了解有关此页面上显示的任何索引的更多信息,请参阅每个部分中提供的链接。

要使用本页中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如 <connection string URI> )替换为 MongoDB 部署的相关值。

您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:

  1. 确保已安装C驱动程序。

  2. 复制以下代码并将其粘贴到新的.c文件中。

  3. 从此页面复制代码示例,并将其粘贴到文件中的指定行。

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

以下示例在指定字段上创建一个升序索引:

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);

要了解有关单字段索引的更多信息,请参阅单字段索引指南。

以下示例在指定字段上创建两个升序索引的复合索引:

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);

要了解有关复合索引的更多信息,请参阅复合索引指南。

以下示例在指定的数组值字段上创建一个升序多键索引:

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);

要了解有关多键索引的更多信息,请参阅多键索引指南。

以下示例在包含GeoJSON对象的指定字段上创建2 dsphere索引:

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);

要学习;了解有关GeoJSON数据类型的更多信息,请参阅MongoDB Server手册中的GeoJSON对象

以下示例在指定字段上创建一个升序唯一索引:

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);

以下示例在指定集合中创建升序通配符索引:

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);

以下示例在 _id字段上创建一个具有升序集群索引的新集合:

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);

以下示例在指定string字段上创建文本索引:

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);

以下示例删除具有指定名称的索引:

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

要了解有关删除索引的更多信息,请参阅《使用索引》指南中的删除索引。

以下部分包含描述如何管理MongoDB搜索索引的示例。

要了解有关MongoDB搜索索引的更多信息,请参阅MongoDB搜索索引指南。

以下示例在指定字段上创建MongoDB搜索索引:

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);

要学习;了解有关创建搜索索引的更多信息,请参阅创建搜索索引指南。

以下示例打印指定集合中的MongoDB搜索索引列表:

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);

要了解有关列出Atlas Search索引的更多信息,请参阅列出Atlas Search索引指南。

以下示例使用指定的新索引定义更新现有MongoDB 搜索索引:

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);

要了解有关更新Atlas Search索引的更多信息,请参阅更新Atlas Search索引指南。

以下示例删除具有指定名称的MongoDB 搜索索引:

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);

要了解有关删除Atlas Search索引的更多信息,请参阅删除Atlas Search索引指南。

在此页面上