Overview
このページでは、 MongoDBにデータを書き込むための一般的なC++ドライバー メソッドを示すコピー可能なコード例があります。
Tip
このページに記載されているメソッドの詳細については、各セクションに提供されているリンクを参照してください。
このページの例を使用するには、コード例をサンプルアプリケーションまたは独自のアプリケーションにコピーします。 <connection string>などのすべてのプレースホルダーを、 MongoDBデプロイに関連する値に置き換えてください。
サンプル アプリケーション
次のサンプルアプリケーションを使用して、このページのコード例をテストできます。 サンプル アプリケーションを使用するには、次の手順を実行します。
プロジェクトが C++ ドライバーをインポートできるロケーションにC++ドライバーがインストールされていることを確認してください。
次のコードをコピーし、プロジェクト内の新しい
.cppファイルに貼り付けます。このページからコード例をコピーし、ファイルのハイライトされたセクション内に貼り付けます。
<iostream>
<bsoncxx/builder/basic/document.hpp> <bsoncxx/json.hpp> <mongocxx/client.hpp> <mongocxx/exception/exception.hpp> <mongocxx/instance.hpp> <mongocxx/uri.hpp>
using bsoncxx::builder::basic::kvp;using bsoncxx::builder::basic::make_document;
int main() { try { mongocxx::instance instance;
mongocxx::uri uri("<connection string>"); mongocxx::client client(uri);
auto database = client["<database name>"]; auto collection = database["<collection name>"];
// Start example code here
// End example code here
} catch (const mongocxx::exception& e) { std::cout << "An exception occurred: " << e.what() << "\n"; return EXIT_FAILURE; } return EXIT_SUCCESS;}1 つを挿入
次のコードは、コレクションに単一のドキュメントを挿入する方法を示しています。
auto result = collection.insert_one(make_document(kvp("<field name>", "<value>")));insert_one()メソッドについて詳しくは、ドキュメントの挿入ガイドをご覧ください。
複数挿入
次のコードは、コレクションに複数のドキュメントを挿入する方法を示しています。
std::vector<bsoncxx::document::value> documents;documents.push_back(make_document(kvp("<field name>", "<value>")));documents.push_back(make_document(kvp("<field name>", "<value>")));
auto result = collection.insert_many(documents);insert_many()メソッドについて詳しくは、ドキュメントの挿入ガイドをご覧ください。
更新 1
次のコードは、フィールドを作成または編集して、コレクション内の 1 つのドキュメントを更新する方法を示しています。
auto query_filter = make_document(kvp("<field to match>", "<value to match>"));auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>"))));
auto result = collection.update_one(query_filter.view(), update_doc.view());update_one()メソッドについて詳しくは、ドキュメントの更新ガイドを参照してください。
複数更新
次のコードは、フィールドを作成または編集して、コレクション内の複数のドキュメントを更新する方法を示しています。
auto query_filter = make_document(kvp("<field to match>", "<value to match>"));auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>"))));
auto result = collection.update_many(query_filter.view(), update_doc.view());update_many()メソッドについて詳しくは、ドキュメントの更新ガイドを参照してください。
replaceOne
次のコードは、コレクション内の 1 つのドキュメントを置き換える方法を示しています。
auto query_filter = make_document(kvp("<field to match>", "<value to match>"));auto replace_doc = make_document(make_document(kvp("<field name>", "<value>")));
auto result = collection.replace_one(query_filter.view(), replace_doc.view());replace_one()メソッドの詳細については、ドキュメントの置換のガイドを参照してください。
deleteOne
次のコードは、コレクション内の 1 つのドキュメントを削除する方法を示しています。
auto result = collection.delete_one(make_document(kvp("<field name>", "<value>")));delete_one()メソッドの詳細については、ドキュメントの削除のガイドを参照してください。
複数削除
次のコードは、コレクション内の複数のドキュメントを削除する方法を示しています。
auto result = collection.delete_many(make_document(kvp("<field name>", "<value>")));delete_many()メソッドの詳細については、ドキュメントの削除のガイドを参照してください。
一括書き込み (write)
次のコードは、1 つの一括操作で複数の書き込み操作を実行する方法を示しています。
auto bulk = collection.create_bulk_write();
auto insert_doc = make_document(kvp("<field name>", "<value>"));auto update_filter = make_document(kvp("<field name>", "<value>"));auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>"))));auto delete_filter = make_document(kvp("<field name>", "<value>"));
mongocxx::model::insert_one insert_op{insert_doc.view()};mongocxx::model::update_many update_op{update_filter.view(), update_doc.view()};mongocxx::model::delete_one delete_op{delete_filter.view()};
bulk.append(insert_op);bulk.append(update_op);bulk.append(delete_op);
auto result = bulk.execute();create_bulk_write()メソッドの詳細については、一括書込みのガイドを参照してください。