Read Documents with VS Code
You can read documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:
Note
You can open a JavaScript Playground pre-configured to search a collection by hovering over the Documents label in the navigation panel and clicking the icon that appears.
Prerequisites
If you have not done so already, you must complete the following prerequisites before you can read documents with a MongoDB Playground:
Create Documents with VS Code or create documents in a collection using a different method.
Read One Document
To read one document, use the following syntax in your Playground:
db.collection.findOne( { <query> }, { <projection> } )
If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.
To learn more about this method's parameters, see findOne() 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.
You may edit any JSON document returned from a findOne() or find() operation.
At the top of this document, click Edit Document. VS Code Extension opens it as an editable EJSON document titled
<database>.<collection>:"<_id>".json
.Make any edits you require.
Press
Ctrl + S
(Windows/Linux) orCmd + S
to save the edited document to the MongoDB database.If the update succeeds, VS Code Extension confirms that the database has stored the change.
If the update results in an error, VS Code Extension displays it.
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.Reads one document in the
test.sales
collection that matches the query.
use("test"); db.sales.findOne( { "_id" : 1 }, { "_id" : 0 } );
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.
{ item: 'abc', price: 10, quantity: 2, date: 2014-03-01T08:00:00.000Z }
Read Many Documents
To read many documents, use the following syntax in your Playground:
db.collection.find( { <query> }, { <projection> } )
For a detailed description of this method's parameters, see find() 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.Reads all documents in the
test.sales
collection that match the query.
use("test"); db.sales.find( { "item" : "abc" }, { "price" : 1 } );
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.
[ { _id: 2, price: 10 }, { _id: 6, price: 10 }, { _id: 9, price: 10 }, { _id: 1, price: 10 } ]