Overview
在此页面上,您可以查看可复制的代码示例,这些示例显示了可用于通过C驱动程序检索文档的常用函数。
提示
要学习;了解有关此页面上显示的任何功能的更多信息,请参阅每个部分中提供的相关指南的链接。
要使用此页面中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如<connection string URI> )替换为 MongoDB 部署的相关值。
示例应用程序
您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:
确保已安装C驱动程序。
复制以下代码并将其粘贴到新的
.c文件中。从此页面复制代码示例,并将其粘贴到文件中的指定行。
<bson/bson.h> <mongoc/mongoc.h> <stdio.h>
intmain(void){ char *str; bson_error_t error;
mongoc_init();
mongoc_client_t *client = mongoc_client_new("<connection string URI>"); mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "collection name");
// Start example code here
// End example code here
mongoc_collection_destroy(collection); mongoc_client_destroy(client); mongoc_cleanup();
return EXIT_SUCCESS;}提示
有关如何安装C驱动程序的说明,请参阅入门指南中的下载和安装。
查找文档
以下示例检索与给定过滤指定的条件相匹配的文档列表:
bson_t *query = bson_new();// Add fields to query heremongoc_cursor_t* results = mongoc_collection_find_with_opts(collection, query, NULL, NULL);const bson_t *doc;
while (mongoc_cursor_next(results, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("%s\n", str); bson_free(str);}
mongoc_cursor_destroy(results);bson_destroy(query);要学习;了解有关mongoc_collection_find_with_opts() 函数的更多信息,请参阅“查找文档”指南。
对集合中的文档进行计数
以下示例返回指定集合中的文档数:
bson_t *query = bson_new();int64_t count = mongoc_collection_count_documents(collection, query, NULL, NULL, NULL, &error);
printf("%" PRId64 "\n", count);bson_destroy(query);要学习;了解有关mongoc_collection_count_documents() 函数的更多信息,请参阅《计数文档》指南中的《检索准确计数》部分。
对查询返回的文档进行计数
以下示例返回符合给定过滤指定条件的文档数:
bson_t *query = bson_new();// Add fields to query hereint64_t count = mongoc_collection_count_documents(collection, query, NULL, NULL, NULL, &error);
printf("%" PRId64 "\n", count);
bson_destroy(query);要学习;了解有关mongoc_collection_count_documents() 函数的更多信息,请参阅《计数文档》指南中的《检索准确计数》部分。
估计文档计数
以下示例根据集合元数据返回指定集合中文档的大致数量:
int64_t count = mongoc_collection_estimated_document_count(collection, NULL, NULL, NULL, &error);
printf("%" PRId64 "\n", count);要学习;了解有关mongoc_collection_estimated_document_count() 函数的更多信息,请参阅《计数文档》指南中的“检索估计计数”部分。
Retrieve Distinct Values
以下示例返回给定集合中指定字段名称的所有非重复值:
bson_t reply;bson_t *command = BCON_NEW("distinct", BCON_UTF8("<collection name>"), "key", BCON_UTF8("<field name>"));
if (!mongoc_collection_read_command_with_opts(collection, command, NULL, NULL, &reply, &error)) { fprintf(stderr, "An error occurred: %s\n", error.message);} else { char *str = bson_as_canonical_extended_json(&reply, NULL); printf("%s\n", str); bson_free(str);}bson_destroy(&reply);bson_destroy(command);监控数据变化
以下示例为给定集合创建变更流,并打印该集合中的后续变更事件:
mongoc_change_stream_t *change_stream;bson_t *pipeline = bson_new();// Add stages to pipeline hereconst bson_t *doc;
change_stream = mongoc_collection_watch(collection, pipeline, NULL);
while (mongoc_change_stream_next(change_stream, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("Change: %s\n", str); bson_free(str);}
bson_destroy(pipeline);mongoc_change_stream_destroy(change_stream);