Docs Menu

複数のドキュメントの挿入

同期InsertMany()メソッドまたは非同期InsertManyAsync()メソッドを使用して、コレクションに複数のドキュメントを挿入できます。

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

AsynchronousSynchronous対応するコードを表示するには、 タブまたは タブを選択します。

// Generates 5 new restaurants by using a helper method
var restaurants = GenerateDocuments();
// Asynchronously inserts the new documents into the restaurants collection
await _restaurantsCollection.InsertManyAsync(restaurants);

InsertManyAsync()操作の完全に実行可能な例えについては、 InsertManyAsync コード サンプルを参照してください。

// Generates 5 new restaurants by using a helper method
var restaurants = GenerateDocuments();
// Inserts the new documents into the restaurants collection
_restaurantsCollection.InsertMany(restaurants);

InsertMany()操作の完全に実行可能な例については、 InsertMany コード サンプルを参照してください。

前述の例のいずれかを実行すると、出力は次のようになります。

Number of restaurants found before insert: 0
Inserting documents...
Number of restaurants inserted: 5

ビルダの使用の詳細については、「 ビルダを使用した操作 」を参照してください。