插入多个文档
您可以使用同步的InsertMany()
方法或异步的 InsertManyAsync()
方法将多个文档插入到集合中。
示例
以下示例将多个文档插入到 restaurants
集合中。
选择异步 或 同步 选项卡以查看相应的代码。
// 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
其他信息
要了解更多关于使用构建器的信息,请参阅使用构建器执行的操作.