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 + Sto 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
testdatabase.Reads one document in the
test.salescollection that matches the query.
use("test"); db.sales.findOne( { "_id" : 1 }, { "_id" : 0 } );
When you press the 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
testdatabase.Reads all documents in the
test.salescollection that match the query.
use("test"); db.sales.find( { "item" : "abc" }, { "price" : 1 } );
When you press the 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 } ]
Read Documents with a Cursor
When the last expression in your Playground evaluates to a cursor, such
as a db.collection.find() or db.collection.aggregate() call,
Visual Studio Code opens the results in the cursor results document browser view.
In this view, you can:
Page through results with the pagination controls at the top of the view.
View each document in a table-like list. Documents appear in shell syntax by default.
Note
Visual Studio Code still uses the regular results editor for
operations that don't produce a cursor with a list of documents, such as
aggregations ending with $out or $merge, or results that are not
documents, such as strings, numbers, or other scalar values.
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
testdatabase.Reads all documents from the
test.salescollection that match the query.Returns a cursor as the last expression.
use("test"); db.sales.find( { "price": { $gte: 10 } }, { "price": 1 } );
When you click the button, Visual Studio Code opens a document browser tab that shows the documents the cursor returned with pagination controls at the top of the view.
Return the Entire Cursor as an Array
If you prefer to convert a cursor to an array and view it directly in
the Playground Results editor instead of the document
browser, call toArray() on the cursor as the last expression
in your Playground:
let cursor = db.collection.find({ <query> }); cursor.toArray();
When you press the 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": 3, "price": 20 }, { "_id": 6, "price": 10 }, { "_id": 9, "price": 10 } ]
Learn More
For more detailed documentation on cursor methods and behaviors, see: