对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

读取数据

在此页面上,您可以查看可复制的代码示例,这些示例显示了可用于通过C驱动程序检索文档的常用函数。

提示

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

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

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

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

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

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

#include <bson/bson.h>#include <mongoc/mongoc.h>#include <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() 函数的更多信息,请参阅《计数文档》指南中的“检索估计计数”部分。

以下示例返回给定集合中指定字段名称的所有非重复值:

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

要学习;了解有关distinct 命令的更多信息,请参阅《检索不同字段值》指南。

以下示例为给定集合创建变更流,并打印该集合中的后续变更事件:

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

要学习;了解有关mongoc_collection_watch() 函数的更多信息,请参阅《监控数据更改》指南。