Insert a Document插入一个文档¶
If you specify a callback method, 如果指定回调方法,insertOne()
returns nothing. insertOne()
将不返回任何内容。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 a document into a collection using the collection.insertOne() method. 可以使用collection.insertOne()
方法将文档插入到集合中。To insert a document, define an object that contains the fields and values that you want to store. 要插入文档,请定义包含要存储的字段和值的对象。If the specified collection does not exist, the 如果指定的集合不存在,则insertOne()
method creates the collection.insertOne()
方法将创建该集合。
You can specify additional query options using the 可以使用options
parameter. options
参数指定其他查询选项。For more information on the method parameters, see the insertOne() API documentation. 有关方法参数的更多信息,请参阅insertOne()
API文档。You can also pass a callback method as an optional third parameter. 还可以将回调方法作为可选的第三个参数传递。For more information on this method, see the insertOne() API documentation.有关此方法的更多信息,请参阅insertOne()
API文档。
If the operation successfully inserts a document, it appends an 如果操作成功插入文档,它将向方法调用中传递的对象追加insertedId
field to the object passed in the method call, and sets the value of the field to the _id
of the inserted document.insertedId
字段,并将该字段的值设置为插入文档的_id
。
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 haiku = database.collection("haiku");
// create a document to insert
const doc = {
title: "Record of a Shriveled Datum",
content: "No bytes, no problem. Just insert a document, in MongoDB",
}
const result = await haiku.insertOne(doc);
console.log(`A document was inserted with the _id: ${result.insertedId}`);
} 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 Haiku {
title: string;
content: string;
}
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 haiku = database.collection<Haiku>("haiku");
const result = await haiku.insertOne({
title: "Record of a Shriveled Datum",
content: "No bytes, no problem. Just insert a document, in MongoDB",
});
console.log(`A document was inserted with the _id: ${result.insertedId}`);
} finally {
await client.close();
}
}
run().catch(console.dir);
If you run the preceding example, you should see the following output:如果运行前面的示例,您应该会看到以下输出:
A document was inserted with the _id: <your _id value>