使用索引优化查询
概述
在本页中,您可以查看可复制的代码示例,展示如何使用C驱动程序管理不同类型的索引。
提示
要了解更多关于索引的工作方式,请参阅使用索引指南。要了解更多关于本页上显示的任何索引,请参阅每个部分提供的链接。
要使用本页的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。请确保将代码示例中的所有占位符,如<连接字符串URI>
,替换为您MongoDB部署的相关值。
示例应用程序
您可以使用以下示例应用程序来测试本页上的代码示例。要使用示例应用程序,请执行以下步骤
确保已安装C驱动程序。
复制以下代码并将其粘贴到一个新的
.c
文件中。从本页复制一个代码示例并将其粘贴到文件中指定的行。
1 2 3 4 5 int 6 main (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 }
单字段索引
以下示例在指定的字段上创建一个升序索引
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对象的指定字段上创建了2dsphere索引
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服务器手册中的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);
文本索引
以下示例在指定的字符串字段上创建一个文本索引
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); }
要了解有关删除索引的更多信息,请参阅《使用索引指南》中的删除索引。
Atlas Search 索引管理
以下部分包含如何管理 Atlas Search 索引的代码示例。
要了解有关 Atlas Search 索引的更多信息,请参阅Atlas Search 索引指南。
创建搜索索引
以下示例展示了如何在指定字段上创建 Atlas Search 索引。
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);
要了解有关创建搜索索引的更多信息,请参阅创建搜索索引指南。
列表搜索索引
以下示例打印指定集合中的 Atlas Search 索引列表
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 索引
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 索引
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);
有关删除搜索索引的更多信息,请参阅删除搜索索引指南。