Docs Menu
Docs Home
/ / /
C#/.NET
/

Insert Multiple Documents

On this page

  • Example
  • Expected Result
  • Additional Information
  • API Documentation

You can insert multiple documents into a collection by using the synchronous InsertMany() method or the asynchronous InsertManyAsync() method.

The following example inserts multiple documents into the restaurants collection.

Select the Asynchronous or Synchronous tab to see the corresponding code.

// 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);

For a fully runnable example of the InsertManyAsync() operation, see the InsertManyAsync code sample.

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

For a fully runnable example of the InsertMany() operation, see the InsertMany code sample.

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

To learn more about using builders, see Operations with Builders.

Back

Insert a Document

Next

Update a Document