You can create documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB游乐场中使用MongoDB CRUD运算符在集合中创建文档:
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游乐场创建文档:
Create One Document创建一个文档
To create one document, use the following syntax in your Playground:要创建一个文档,请在游乐场中使用以下语法:
db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)
Note
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. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.要运行游乐场,请按游乐场视图右上角的播放按钮。VS Code扩展拆分游乐场,并在“游乐场Results.json”窗格中输出游乐场的结果。如果禁用拆分视图,VS Code扩展将在新选项卡中输出游乐场的结果。
Example示例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB游乐场开始,如果加载了模板游乐场,请清除它。
The following example:以下示例:
Switches to the切换到testdatabase.test数据库。Inserts eight documents into the将八个文档插入到test.salescollection.test.sales集合中。
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, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,VS Code扩展会拆分游乐场,并在“游乐场Results.json”窗格中输出以下文档。If you disabled split-view, VS Code Extension outputs the following document in a new tab. If you manually move your playground results, VS Code Extension displays the results in that tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出以下文档。如果手动移动游乐场结果,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>
}
)
Note
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. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.要运行游乐场,请按游乐场视图右上角的“播放”按钮。VS Code扩展拆分游乐场,并在游乐场Results.json窗格中输出游乐场的结果。如果禁用拆分视图,VS Code扩展将在新选项卡中输出游乐场的结果。
Example示例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB游乐场开始,如果加载了模板游乐场,请清除它。
The following example:以下示例:
Switches to the切换到testdatabase.test数据库。Inserts eight documents into the将八个文档插入到test.salescollection.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, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,VS Code扩展会拆分游乐场,并在“游乐场Results.json”窗格中输出以下文档。If you disabled split-view, VS Code Extension outputs the following document in a new tab. If you manually move your playground results, VS Code Extension displays the results in that tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出以下文档。如果手动移动游乐场结果,VS Code延伸将在该选项卡中显示结果。
{
acknowleged: 1,
insertedIds: {
'0': 2,
'1': 3,
'2': 4,
'3': 5,
'4': 6,
'5': 7,
'6': 8,
'7': 9
}
}