使用索引优化查询
在此页面上
Overview
在此页面上,您可以查看可复制的代码示例,这些示例展示了如何使用C驾驶员管理不同类型的索引。
提示
要学习;了解有关使用索引的更多信息,请参阅使用索引指南。 要学习;了解有关此页面上显示的任何索引的更多信息,请参阅每个部分中提供的链接。
要使用本页中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如 <connection string 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);
要了解有关复合索引的更多信息,请参阅复合索引指南。
Multikey Index
以下示例在指定的数组值字段上创建一个升序多键索引:
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);
通配符索引(Wildcard Index)
以下示例在指定集合中创建升序通配符索引:
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);
Text Index
以下示例在指定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); }
要了解有关删除索引的更多信息,请参阅《使用索引》指南中的删除索引。
Atlas Search 索引管理
以下部分包含描述如何管理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索引的更多信息,请参阅列出Atlas Search索引指南。
更新搜索索引
以下示例使用指定的新索引定义更新现有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索引的更多信息,请参阅更新Atlas Search索引指南。
删除Atlas Search索引
以下示例删除具有指定名称的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);
要了解有关删除Atlas Search索引的更多信息,请参阅删除Atlas Search索引指南。