Docs Menu
Docs Home
/
MongoDB for VS Code
/ /

Read Documents with VS Code

On this page

  • Prerequisites
  • Read One Document
  • Read Many Documents

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

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

Back

Create