Insert Multiple Documents插入多个文档¶
If you specify a callback method, 如果指定回调方法,insertMany()
returns nothing. insertMany()
将不返回任何内容。If you do not specify one, this method returns a 如果未指定,此方法将返回一个Promise
that resolves to the result object when it completes. Promise
,该Promise
在完成时解析为结果对象。See our guide on Promises and Callbacks for more information, or the API documentation for information on the result object.有关更多信息,请参阅我们的承诺和回调指南,或参阅API文档以了解有关输出对象的信息。
You can insert multiple documents using the collection.insertMany() method. 可以使用collection.insertMany()方法插入多个文档。The insertMany()
takes an array of documents to insert into the specified collection.insertMany()
接受要插入到指定集合中的文档数组。
You can specify additional options in the 可以在作为options
object passed as the second parameter of the insertMany()
method. insertMany()
方法的第二个参数传递的options对象中指定其他选项。Specify 指定ordered:true
to prevent inserting the remaining documents if the insertion failed for a previous document in the array.ordered:true
以防止在数组中的前一个文档插入失败时插入其余文档。
Specifying incorrect parameters for your 为insertMany()
operation can cause problems. insertMany()
操作指定不正确的参数可能会导致问题。Attempting to insert a field to a value that would violate unique index rules will throw a 尝试向违反唯一索引规则的值插入字段将引发“重复键”错误。duplicate key error
.
Example示例¶
You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. 您可以使用此示例连接到MongoDB实例,并与包含示例数据的数据库交互。To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.要了解有关连接到MongoDB实例并加载示例数据集的更多信息,请参阅用法示例指南。
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.用MongoDB部署的连接字符串替换uri字符串。
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
const database = client.db("insertDB");
const foods = database.collection("foods");
// create an array of documents to insert创建要插入的文档数组
const docs = [
{ name: "cake", healthy: false },
{ name: "lettuce", healthy: true },
{ name: "donut", healthy: false }
];
// this option prevents additional documents from being inserted if one fails此选项可防止在文档失败时插入其他文档
const options = { ordered: true };
const result = await foods.insertMany(docs, options);
console.log(`${result.insertedCount} documents were inserted`);
} finally {
await client.close();
}
}
run().catch(console.dir);
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.用MongoDB部署的连接字符串替换uri字符串。
const uri = "<connection string uri>";
const client = new MongoClient(uri);
interface Food {
name: string;
healthy: boolean;
}
async function run() {
try {
await client.connect();
const database = client.db("insertDB");
// Specifying a schema is optional, but it enables type hints on finds and inserts指定架构是可选的,但它在查找和插入时启用类型提示
const foods = database.collection<Food>("foods");
const result = await foods.insertMany(
[
{ name: "cake", healthy: false },
{ name: "lettuce", healthy: true },
{ name: "donut", healthy: false },
],
{ ordered: true }
);
console.log(`${result.insertedCount} documents were inserted`);
} finally {
await client.close();
}
}
run().catch(console.dir);
If you run the preceding example, you should see the following output:如果运行前面的示例,您应该会看到以下输出:
3 documents were inserted