Docs Menu
Docs Home
/ /

Read Documents with VS Code

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

  • Use the findOne() method to read one document.

  • Use the find() method to read more than one document.

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.

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

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.

  1. At the top of this document, click Edit Document. VS Code Extension opens it as an editable EJSON document titled <database>.<collection>:"<_id>".json.

  2. Make any edits you require.

  3. Press Ctrl + S (Windows/Linux) or Cmd + 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.

To run 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. 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 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
}

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.

To run 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. 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 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
}
]

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.

To run 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. Reads all documents from the test.sales collection that match the query.

  3. 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.

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
}
]

For more detailed documentation on cursor methods and behaviors, see:

Back

Create

On this page