Docs 菜单
Docs 主页
/ /

索引

在本指南中,您可以了解如何在 C 驱动程序中使用索引。索引可以提高查询的效率,并为查询和存储文档增加功能。

如果没有索引,MongoDB 必须扫描集合中的每个文档,以查找与每个查询匹配的文档。 这些集合扫描很慢,可能会对应用程序的性能产生负面影响。 但是,如果查询存在适当的索引,MongoDB 就可以使用该索引来限制必须检查的文档。

要提高查询性能,请对应用程序查询中经常出现的字段以及返回排序结果的操作构建索引。 您添加的每个索引在活动状态下都会消耗磁盘空间和内存,因此我们建议您追踪索引内存和磁盘使用情况以进行容量规划。 此外,当写入操作更新索引字段时, MongoDB会更新相关索引,这可能会对写入操作的性能产生负面影响。

您可以在MongoDB应用程序中使用通配符索引来查询事先未知名称或任意名称的字段。 通配符索引并不是为了取代基于工作负载的索引规划而设计的。

有关设计数据模型和选择适合应用程序的索引的更多信息,请参阅MongoDB Server手册中的数据建模和索引指南。

本指南中的示例使用Atlas示例数据集的 数据库中的 moviestheaterssample_mflix 集合。要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。

MongoDB支持多种不同的索引类型来帮助查询数据。 以下页面介绍了最常见的索引类型,并提供了创建每种索引类型的示例代码。

  • 单字段索引

  • 复合索引

  • Multikey Indexes

  • MongoDB搜索索引

以下部分介绍了其他常见索引类型,并提供了创建每种索引类型的示例代码。

以下示例在 location.geo字段上创建 2dsphere索引,其中包含 theaters集合中的GeoJSON对象:

bson_error_t error;
bson_t *keys = BCON_NEW("location.geo", 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对象

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

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

以下示例在 title 字符串字段上创建文本索引:

bson_error_t error;
bson_t *keys = BCON_NEW("title", 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);

您可以删除_id字段上除默认唯一索引之外的任何未使用的索引。 将 mongoc_collection_t 结构、索引名称和可选的 bson_error_t 结构传递给 mongoc_collection_drop_index() 函数,以从集合中删除索引。

以下示例从movies集合中删除名为"_title_"的索引:

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

注意

不能从复合文本索引中删除单个字段。您必须删除整个索引,然后创建新索引,才能更新索引字段。

要学习;了解有关本指南中讨论的任何函数的更多信息,请参阅以下API文档:

后退

时间序列

在此页面上