文档菜单
文档首页
/ / /
C 驱动器

将数据写入 MongoDB

本页内容

  • 概述
  • 示例应用程序
  • 插入单个文档
  • 插入多个文档
  • 更新单个文档
  • 更新多个文档
  • 删除单个文档
  • 删除多个文档
  • 批量写入

在本页中,您可以查看可复制代码示例,展示您可以使用C驱动程序将数据写入MongoDB的常用函数。

提示

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

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

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

  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 =
16 mongoc_client_new ("<connection string URI>");
17 collection =
18 mongoc_client_get_collection (client, "<database name>", "collection name");
19
20 // Start example code here
21
22 // End example code here
23
24 mongoc_collection_destroy (collection);
25 mongoc_client_destroy (client);
26 mongoc_cleanup ();
27
28 return EXIT_SUCCESS;
29}

以下代码演示如何将单个文档插入到集合中

bson_t *document = BCON_NEW ("<field name>", BCON_UTF8 ("<value>"));
bson_error_t error;
if (!mongoc_collection_insert_one (
collection, document, NULL, NULL, &error)) {
fprintf (stderr, "Insert one operation failed: %s\n", error.message);
}
bson_destroy (document);

要了解有关mongoc_collection_insert_one()函数的更多信息,请参阅插入文档指南。

以下代码展示了如何将多个文档插入集合

size_t num_docs = 2;
bson_t *docs[num_docs];
docs[0] = BCON_NEW ("<field name>", BCON_UTF8 ("<value>"));
docs[1] = BCON_NEW ("<field name>", BCON_UTF8 ("<value>"));
bson_error_t error;
if (!mongoc_collection_insert_many (collection, (const bson_t **) docs, num_docs, NULL, NULL, &error)) {
fprintf (stderr, "Insert many operation failed: %s\n", error.message);
}
bson_destroy (docs[0]);
bson_destroy (docs[1]);

要了解更多关于 mongoc_collection_insert_many() 函数的信息,请参阅插入文档指南。

以下代码展示了如何通过创建或编辑字段来更新集合中的一个单个文档

bson_t *query = BCON_NEW ("<field to match>", BCON_UTF8 ("<value to match>"));
bson_t *update = BCON_NEW ("$set", "{", "<field name>", BCON_UTF8 ("<value>"), "}");
bson_error_t error;
if (!mongoc_collection_update_one (collection, query, update, NULL, NULL, &error)) {
fprintf (stderr, "Update one operation failed: %s\n", error.message);
}
bson_destroy (query);
bson_destroy (update);

要了解更多关于 mongoc_collection_update_one() 函数的信息,请参阅更新文档指南。

以下代码展示了如何通过创建或编辑字段来更新集合中的多个文档

bson_t *query = BCON_NEW ("<field to match>", BCON_UTF8 ("<value to match>"));
bson_t *update = BCON_NEW ("$set", "{", "<field name>", BCON_UTF8 ("<value>"), "}");
bson_error_t error;
if (!mongoc_collection_update_many (collection, query, update, NULL, NULL, &error)) {
fprintf (stderr, "Update many operation failed: %s\n", error.message);
}
bson_destroy (query);
bson_destroy (update);

要了解更多关于 mongoc_collection_update_many() 函数的信息,请参阅更新文档指南。

以下代码展示了如何在集合中删除单个文档

bson_t *filter = BCON_NEW ("<field name>", BCON_UTF8 ("<value>"));
bson_error_t error;
if (!mongoc_collection_delete_one (collection, filter, NULL, NULL, &error)) {
fprintf (stderr, "Delete error: %s\n", error.message);
}
bson_destroy (filter);

要了解更多关于 mongoc_collection_delete_one() 函数的信息,请参阅删除文档指南。

以下代码展示了如何在集合中删除多个文档

bson_t *filter = BCON_NEW ("<field name>", BCON_UTF8 ("<value>"));
bson_error_t error;
if (!mongoc_collection_delete_many (collection, filter, NULL, NULL, &error)) {
fprintf (stderr, "Delete error: %s\n", error.message);
}
bson_destroy (filter);

要了解更多关于 mongoc_collection_delete_many() 函数的信息,请参阅删除文档指南。

以下代码展示了如何在单个批量操作中执行多个写入操作

bson_error_t error;
mongoc_bulk_operation_t *bulk =
mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
bson_t *insert_doc = BCON_NEW (
"<field name>", BCON_UTF8 ("<value>"),
"<field name>", BCON_UTF8 ("<value>"),
"<field name>", BCON_UTF8 ("<value>"),
"<field name>", BCON_UTF8 ("<value>")
);
mongoc_bulk_operation_insert(bulk, insert_doc);
bson_destroy (insert_doc);
bson_t *query = BCON_NEW ("<field to match>", BCON_UTF8 ("<value to match>"));
bson_t *update = BCON_NEW ("$set", "{", "<field name>", BCON_UTF8 ("<value>"), "}");
mongoc_bulk_operation_update_one(bulk, query, update, false);
bson_destroy(query);
bson_destroy(update);
bool result = mongoc_bulk_operation_execute(bulk, NULL, &error);
if (!result) {
fprintf (stderr, "Bulk operation error: %s\n", error.message);
}
mongoc_bulk_operation_destroy (bulk);

要了解更多关于 mongoc_collection_bulk_operation_execute() 函数的信息,请参阅批量写入操作指南。

返回

运行数据库命令