Docs Menu

Docs HomeMongoDB for VS Code

Navigate Your Data

On this page

  • View Databases and Collections
  • View Collection Documents and Schema
  • Manage Indexes
  • Create a New Database
  • Create a New Collection
  • Drop a Database or Collection
  • Refresh Data

Once you connect to your deployment using MongoDB for VS Code, use the left navigation to:

  • Explore your databases, collections, read-only views, and documents.

  • Create new databases and collections.

  • Drop databases and collections.

Image showing deployment navigation

Note

If your deployment requires authentication, your database user privileges may affect the actions you can perform using MongoDB for VS Code.

When you expand an active connection, MongoDB for VS Code shows the databases in that deployment.

  • Click the name of a database to view the collections it contains.

  • Click the name of a collection to view its documents, schema, and indexes.

Note

MongoDB for VS Code closes all documents when you close Visual Studio Code.

When you expand a collection, MongoDB for VS Code displays the number of documents next to the Documents label in the navigation panel.

When you expand a collection's documents, MongoDB for VS Code lists the _id of each document in the collection. Click an _id value to open that document in Visual Studio Code and view its contents.

You may edit this single document:

  1. At the top of this document, click Edit Document. MongoDB for VS Code 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, MongoDB for VS Code confirms that the database has stored the change.

    • If the update results in an error, MongoDB for VS Code displays it.

Important

Users must have the listCollections permission in order to view a collection's documents.

To view all of the collection's documents in an array, you can:

  1. Right-click a collection.

  2. Click View Documents.

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.

Your collection's schema defines the fields and data types within the collection. Due to MongoDB's flexible schema model, different documents in a collection may contain different fields, and data types may vary within a field. MongoDB can enforce schema validation to ensure your collection documents have the same shape.

When you expand a collection's schema, MongoDB for VS Code lists the fields which appear in that collection's documents. If a field exists in all documents and its type is consistent throughout the collection, MongoDB for VS Code displays an icon indicating that field's data type. Hover over the field name for a text description of the field's data type.

Your collections's indexes are listed under the Indexes heading. When you expand an index, each index key appears with an icon designating its type. Index key types include:

  • Ascending

  • Descending

  • Geospatial (2d, 2dsphere, geoHaystack)

  • Text

  • Hashed

Note

You can open a MongoDB Playground pre-configured to create an index by hovering over the Indexes label in the navigation panel and clicking the icon that appears.

Tip

See also:

To learn more about MongoDB indexes, see the server manual.

When you create a new database, you must populate it with an initial collection.

  1. Hover over the connection for the deployment where you want your database to exist.

  2. Click the icon or right-click and select Add Database. MongoDB for VS Code opens a new tab with const variables for database and collection names.

  3. Update the database and collection variables with the names for your database and collection.

  4. Click the button located at the top right of the tab to execute the script. If the database and collection do not already exist, they are created.

  1. Hover over the database name where you want your collection to exist.

  2. Right-click the database name and select Add Collection. MongoDB for VS Code opens a new tab with const variables for database and collection names.

  3. Update the collection variable with your new collection name.

  4. Click the button located at the top right of the tab to execute the script. The collection is created in the specified database.

Important

Dropping data from MongoDB is an irreversible process. Take caution to only drop data you are sure you want to delete, and backup your data as necessary.

Dropping a database also drops all collections and documents within that database.

To drop a database or collection:

  1. Right-click the target database or collection.

  2. Click Drop Database or Drop Collection.

  3. In the prompt, type the name of the target database or collection.

  4. Press the enter key.

You can refresh a deployment, database, or collection at any time to re-query your deployment and populate MongoDB for VS Code with the most up-to-date data.

To refresh:

  1. Right-click the target deployment, database, or collection.

  2. Click Refresh.

←  Connect to Your MongoDB DeploymentExplore Your Data with Playgrounds →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.