Insert Multiple Documents
This version of the documentation is archived and no longer supported. View the current documentation to learn how to upgrade your version of the .NET/C# Driver.
You can insert multiple documents into a collection by using the synchronous
InsertMany()
method or the asynchronous InsertManyAsync()
method.
Example
The following example inserts multiple documents into
the restaurants
collection.
Select the Asynchronous or Synchronous tab to see the corresponding code.
// Helper method to generate 5 new restaurants var restaurants = GenerateDocuments(); await _restaurantsCollection.InsertManyAsync(restaurants);
For a fully runnable example of the InsertManyAsync()
operation, see the
InsertManyAsync code sample.
// Helper method to generate 5 new restaurants var restaurants = GenerateDocuments(); _restaurantsCollection.InsertMany(restaurants);
For a fully runnable example of the InsertMany()
operation, see the
InsertMany code sample.
Expected Result
After running either of the preceding full examples, the output is as follows:
Number of restaurants found before insert: 0 Inserting documents... Number of restaurants inserted: 5
Additional Information
To learn more about using builders, see Operations with Builders.