可以使用同步 InsertMany() 方法或异步 InsertManyAsync() 方法将多个文档插入到集合中。
例子
以下示例将多个文档插入 restaurants 集合。
选择 Asynchronous 或 Synchronous 标签页,查看相应的代码。
// Generates 5 new restaurants by using a helper methodvar restaurants = GenerateDocuments();
// Asynchronously inserts the new documents into the restaurants collectionawait _restaurantsCollection.InsertManyAsync(restaurants);有关 InsertManyAsync() 操作的完全可运行示例,请参阅 InsertManyAsync 代码示例。
// Generates 5 new restaurants by using a helper methodvar 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更多信息
如需了解有关使用生成器的更多信息,请参阅生成器操作。