On this page本页内容
You can create documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB演练场中使用MongoDB CRUD运算符在集合中创建文档:
If you have not done so already, you must complete the following prerequisites before you can create documents with a MongoDB Playground:如果尚未完成此操作,则必须先完成以下先决条件,然后才能使用MongoDB游乐场创建文档:
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 outputs the results of your playground to the Output view in Visual Studio Code.MongoDB forVS Code将演练场的结果输出到Visual Studio Code中的输出视图。
To use this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要使用此示例,请从一个空白的MongoDB演练场开始,方法是清除模板演练场(如果已加载)。
The following example:以下示例:
test
database.test
数据库。test.sales
collection.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, this operation outputs the following document to the Output view in Visual Studio Code:按下播放按钮时,此操作将以下文档输出到Visual Studio Code中的输出视图:
{
acknowleged: 1,
insertedId: 1
}
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 outputs the results of your playground to the Output view in Visual Studio Code.MongoDB forVS Code将游乐场的结果输出到Visual Studio Code中的输出视图。
To use this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要使用此示例,请从一个空白的MongoDB演练场开始,方法是清除模板演练场(如果已加载)。
The following example:以下示例:
test
database.test
数据库。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, this operation outputs the following document to the Output view in Visual Studio Code:按下播放按钮时,此操作将以下文档输出到Visual Studio Code中的输出视图:
{
acknowleged: 1,
insertedIds: {
'0': 2,
'1': 3,
'2': 4,
'3': 5,
'4': 6,
'5': 7,
'6': 8,
'7': 9
}
}