Create Documents with VS Code
You can create documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:
Use the insertOne() method to insert one document.
Use the insertMany() method to insert more than one document.
Prerequisites
If you have not done so already, you must complete the following prerequisites before you can create documents with a MongoDB Playground:
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.
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.
Example
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.
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, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 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.
{ 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.
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.
Example
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.
The following example:
Switches to the
test
database.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, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 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.
{ acknowleged: 1, insertedIds: { '0': 2, '1': 3, '2': 4, '3': 5, '4': 6, '5': 7, '6': 8, '7': 9 } }