文档菜单
文档首页
/ / /
C 驱动程序

从 MongoDB 读取数据

本页内容

  • 概述
  • 示例应用程序
  • 查找文档
  • 在集合中计数文档
  • 从查询中返回的文档计数
  • 估计文档计数
  • 检索不同的值
  • 监控数据更改

在本页中,您可以查看可复制的代码示例,展示您可以使用C驱动程序检索文档的常见函数。

提示

要了解更多关于本页上显示的任何函数的信息,请参阅每个部分中提供的相关指南链接。

要使用本页上的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。请确保用MongoDB部署的相关值替换代码示例中的所有占位符,例如<连接字符串URI>

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

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

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

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

1#include <bson/bson.h>
2#include <mongoc/mongoc.h>
3#include <stdio.h>
4
5int
6main (void)
7{
8 mongoc_client_t *client;
9 mongoc_collection_t *collection;
10 char *str;
11 bson_error_t error;
12
13 mongoc_init ();
14
15 client = mongoc_client_new ("<connection string URI>");
16 collection = mongoc_client_get_collection (client, "<database name>", "collection name");
17
18 // Start example code here
19
20 // End example code here
21
22 mongoc_collection_destroy (collection);
23 mongoc_client_destroy (client);
24 mongoc_cleanup ();
25
26 return EXIT_SUCCESS;
27}

提示

有关安装C驱动程序的说明,请参阅下载和安装指南中的说明。

以下示例检索符合给定过滤器条件的文档列表

bson_t *query = bson_new ();
// Add fields to query here
mongoc_cursor_t* results = mongoc_collection_find_with_opts (collection, query, NULL, NULL);
const bson_t *doc;
while (mongoc_cursor_next (results, &doc)) {
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 here
int64_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 {
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 here
const bson_t *doc;
change_stream = mongoc_collection_watch (collection, pipeline, NULL);
while (mongoc_change_stream_next (change_stream, &doc)) {
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() 函数的信息,请参阅 监控数据变化 指南。

返回

存储大文件