Docs Menu
Docs Home
/ / /
C++ ドライバー
/

ドキュメントの挿入

項目一覧

  • Overview
  • サンプル データ
  • _id フィールド
  • 1つのドキュメントの挿入
  • 複数のドキュメントの挿入
  • 挿入動作の変更
  • 詳細情報
  • API ドキュメント

このガイドでは、 C++ドライバーを使用して挿入操作を実行し、 MongoDBコレクションにドキュメントを追加する方法を学習できます。

挿入操作は 、1 つ以上の ドキュメント をMongoDBコレクションに挿入します。 挿入操作を実行するには、 insert_one()メソッドを使用して 1 つのドキュメントを挿入し、 insert_many()メソッドを使用して 1 つ以上のドキュメントを挿入します。

このガイドの例では、 Atlasサンプルデータセットsample_restaurants.restaurantsコレクションを使用します。 C++アプリケーションからこのコレクションにアクセスするには、Atlasclient クラスターに接続する をインスタンス化し、次の値をdb 変数とcollection 変数に割り当てます。

auto db = client["sample_restaurants"];
auto collection = db["restaurants"];

MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。

MongoDB コレクションでは、各ドキュメントに一意のフィールド値を持つ_idフィールドが含まれている必要があります。

MongoDB では、このフィールドは次の 2 つの方法で管理できます。

  • このフィールドは各ドキュメントに自分で設定することができ、各_idフィールド値が一意であることを確認します。

  • ドライバーを使用して、各ドキュメント_idに対して一意のObjectId値を自動的に生成できます。

一意性を保証できない限り、ドライバーに_id値を自動的に生成させることをお勧めします。

注意

重複した_id値はユニークインデックス制約に違反するため、ドライバーはmongocxx::bulk_write_exceptionエラーを返します。

_idフィールドの詳細については、 マニュアルの 「 一意なインデックス 」MongoDB Server のガイドを参照してください。

ドキュメント構造とルールの詳細については、MongoDB Server マニュアルのドキュメントガイド を参照してください。

MongoDB コレクションに単一のドキュメントを追加するには、 insert_one()メソッドを呼び出して、追加するドキュメントを渡します。

次の例では、 restaurantsコレクションにドキュメントを挿入します。

auto result = collection.insert_one(make_document(kvp("name", "Mongo's Burgers")));

MongoDBコレクションに複数のドキュメントを追加するには 、 insert_many()メソッドを呼び出し、追加するドキュメントを保存するベクトルを渡します。

次の例では、 restaurantsコレクションに 2 つのドキュメントを挿入しています。

std::vector<bsoncxx::document::value> restaurants;
restaurants.push_back(make_document(kvp("name", "Mongo's Burgers")));
restaurants.push_back(make_document(kvp("name", "Mongo's Pizza")));
auto result = collection.insert_many(restaurants);

mongocxx::options::insertクラスのインスタンスを任意のパラメータとして渡すことで、 insert_one()メソッドとinsert_many()メソッドの動作を変更できます。 次の表では、 mongocxx::options::insertインスタンスに設定できるフィールドを説明しています。

フィールド
説明
bypass_document_validation
If set to true, allows the write to opt out of document-level validation.
Defaults to false.
Type: bool
write_concern
Sets the write concern for the operation.
Defaults to the write concern of the namespace.
Type: mongocxx::write_concern
ordered
If set to true, the operation stops inserting documents when one insert fails. If false, the operation continues to insert the remaining documents when one insert fails.
Defaults to true.
Type: bool
comment
A comment to attach to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.
Type: bsoncxx::types::bson_value::view_or_value

次のコードでは、 insert_many()メソッドを使用して、3 つの新しいドキュメントをコレクションに挿入します。 mongocxx::options::insertインスタンスではbypass_document_validationフィールドがtrueに設定されているため、この挿入操作はドキュメントレベルの検証をバイパスします。

std::vector<bsoncxx::document::value> docs;
docs.push_back(make_document(kvp("name", "Mongo's Burgers")));
docs.push_back(make_document(kvp("name", "Mongo's Pizza")));
docs.push_back(make_document(kvp("name", "Mongo's Tacos")));
mongocxx::options::insert opts;
opts.bypass_document_validation(true);
auto result = collection.insert_many(docs, opts);

C++ドライバーを使用してドキュメントを挿入する実行可能なコード例については、「 MongoDBへのデータの書込み 」を参照してください。

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。

戻る

MongoDB へのデータの書込み (write)