Docs Menu

Create Documents

On this page

  • Prerequisites
  • Create One Document
  • Create Many Documents

You can create documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:

If you have not done so already, you must complete the following prerequisites before you can create documents with a MongoDB Playground:

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.

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. If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.

To use this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.

The following example:

  1. Switches to the test database.
  2. 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. 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.

{
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>
}
)
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.

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. If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.

To use this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.

The following example:

  1. Switches to the test database.
  2. Inserts eight documents into the test.sales collection.
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. 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.

{
acknowleged: 1,
insertedIds: {
'0': 2,
'1': 3,
'2': 4,
'3': 5,
'4': 6,
'5': 7,
'6': 8,
'7': 9
}
}
Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.