Docs Home → MongoDB for VS Code
Create Documents创建文档
On this page本页内容
You can create documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB Playground中使用MongoDB CRUD Operators在集合中创建文档:
Use the insertOne() method to insert one document.使用insertOne()
方法插入一个文档。Use the insertMany() method to insert more than one document.使用insertMany()
方法可以插入多个文档。
Prerequisites先决条件
If you have not done so already, you must complete the following prerequisites before you can create documents with a MongoDB Playground:如果您还没有这样做,您必须完成以下先决条件,然后才能使用MongoDB Playground创建文档:
Create One Document创建一个文档
To create one document, use the following syntax in your Playground:要创建一个文档,请在演练场中使用以下语法:
db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)
If the database doesn't exist, insert operations will create it.如果数据库不存在,插入操作将创建它。
For a detailed description of this method's parameters, see insertOne() in the MongoDB Manual.有关此方法参数的详细描述,请参阅MongoDB手册中的insertOne()。
To run your Playground, press the Play Button at the top right of the Playground View. MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. 要运行您的演练场,请按演练场视图右上角的“播放”按钮。MongoDB for VS Code拆分您的演练场,并在Playground Results.json
窗格中输出演练场的结果。If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出您的演练场的结果。
Example实例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB Playground开始,如果加载了模板Playground,请清除该模板。
The following example:以下示例:
- Switches to the
test
database. - Inserts eight documents into the
test.sales
collection.
use("test");
db.sales.insertOne(
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : new Date("2014-03-01T08:00:00Z")}
);
When you press the Play Button, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,MongoDB for VS Code会拆分您的Playground,并在Playground Results.json
窗格中输出以下文档。If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab. If you manually move your playground results, MongoDB for VS Code displays the results in that tab.如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出以下文档。如果您手动移动演练场结果,MongoDB of VS Code会显示该选项卡中的结果。
{
acknowleged: 1,
insertedId: 1
}
Create Many Documents创建多个文档
To create many documents, use the following syntax in your Playground:要创建多个文档,请在演练场中使用以下语法:
db.collection.insertMany(
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
If the database doesn't exist, insert operations will create it.如果数据库不存在,插入操作将创建它。
For a detailed description of this method's parameters, see insertMany() in the MongoDB Manual.有关此方法参数的详细描述,请参阅MongoDB手册中的insertMany()
。
To run your Playground, press the Play Button at the top right of the Playground View. 要运行您的演练场,请按演练场视图右上角的“播放”按钮。MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. MongoDB for VS Code拆分您的演练场,并在Playground Results.json
窗格中输出演练场的结果。If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出您的演练场的结果。
Example实例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB Playground开始,如果加载了模板Playground,请清除该模板。
The following example:以下示例:
Switches to the切换到test
database.test
数据库。Inserts eight documents into the在test.sales
collection.test.sales
集合中插入八个文档。
use("test");
db.sales.insertMany([
{ "_id" : 2, "item" : "abc", "price" : 10, "quantity" : 2, "date" : new Date("2014-03-01T08:00:00Z") },
{ "_id" : 3, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : new Date("2014-03-01T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : new Date("2014-03-15T09:00:00Z") },
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : new Date("2014-04-04T11:21:39.736Z") },
{ "_id" : 6, "item" : "abc", "price" : 10, "quantity" : 10, "date" : new Date("2014-04-04T21:23:13.331Z") },
{ "_id" : 7, "item" : "def", "price" : 7.5, "quantity": 5, "date" : new Date("2015-06-04T05:08:13Z") },
{ "_id" : 8, "item" : "def", "price" : 7.5, "quantity": 10, "date" : new Date("2015-09-10T08:43:00Z") },
{ "_id" : 9, "item" : "abc", "price" : 10, "quantity" : 5, "date" : new Date("2016-02-06T20:20:13Z") },
]);
When you press the Play Button, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,MongoDB for VS Code会拆分您的演练场,并在Playground Results.json
窗格中输出以下文档。If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab. 如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出以下文档。If you manually move your playground results, MongoDB for VS Code displays the results in that tab.如果手动移动演练场结果,MongoDB for VS Code会在该选项卡中显示结果。
{
acknowleged: 1,
insertedIds: {
'0': 2,
'1': 3,
'2': 4,
'3': 5,
'4': 6,
'5': 7,
'6': 8,
'7': 9
}
}