Docs Menu
Docs Home
/
MongoDB for VS Code

Connect to Your MongoDB Deployment

On this page

  • Considerations
  • Create a Connection
  • Connect to Atlas Stream Processing
  • Activate a Connection
  • Edit a Connection
  • Copy a Connection
  • Rename a Connection
  • Disconnect from a Deployment
  • Remove a Connection

This page outlines how to use MongoDB for VS Code to connect to a MongoDB host. You can connect to a standalone, replica set, or sharded cluster host.

Note

MongoDB Atlas

If you need to create a MongoDB host, consider using MongoDB Atlas. Atlas is a cloud-hosted database-as-a-service which requires no installation, offers a free tier to get started, and provides a copyable URI to easily connect MongoDB for VS Code to your deployment.

To create a cluster on the Atlas free tier, run the command MongoDB: Open Overview Page in the Visual Studio Code Command Palette and then click Create free cluster.

To create an Atlas cluster using a Terraform template included with MongoDB for VS Code, see Create an Atlas Cluster from a Template using Terraform.

If you need to install MongoDB for VS Code, see Install MongoDB for VS Code for instructions.

  • When connecting MongoDB for VS Code to a replica set, use either the replica set SRV record or Replica Set Name when filling in your connection information. It is not recommended to connect directly to an individual replica set member.

    • If the member to which you are connected switches from a primary member to a secondary or vice versa as the result of an election, MongoDB for VS Code may either forcibly close the connection or display stale data.

  • You can't connect MongoDB for VS Code directly to an analytics node.

  • MongoDB for VS Code appends the appName connection string option with a value of mongodb-vscode <version> for all deployment connections.

MongoDB for VS Code provides two options to connect to your deployment:

Connection Method
Description
Connect with Connection String
This method is faster and easier than using the advanced connection settings. Use this option if your deployment is hosted on Atlas or you already have a connection string for a deployment available.
Advanced Connection Settings
This method lets you build a connection string with a form. Use this option if you need to customize the connection string and want to see available connection options.

Note

To specify where MongoDB for VS Code connections are saved, use the Default Connection Saving Location setting.

New in version 1.50.

You can use MongoDB for VS Code to connect to Atlas Stream Processing by providing a Stream Processing Interface (SPI) connection string.

For details on how to configure Atlas Stream Processing, see Configure a Stream Processing Instance.

1
  1. Expand the Connections pane in the left navigation if it is collapsed.

  2. Click the More Actions menu (...) and select Add MongoDB Connection with Connection String...

2

To obtain an SPI connection string login to your Atlas account. Click Stream Processing from the left-hand navigation and then select a Stream Processing Instance. Click Connect and choose MongoDB for VSCode from the Choose a Connection Method page of the connect dialog.

Tip

For full details on using and configuring Atlas Stream Processing, see Atlas Stream Processing Overview.

The following code block is an example of SPI connection string:

mongodb://user1:password1@atlas-stream-xxxxxxxxxxxxxxxxxxxxxxxx-yyyyy.virginia-usa.a.query.mongodb.net/?authSource=admin&readPreference=primary&ssl=true&directConnection=true

Once you are connected, the stream connection displays under the Connections pane:

Image of a stream connection
click to enlarge
3

After successfully connecting to your Atlas Stream, click Create New Playground on the Playgrounds pane. A stream processing playground template displays with pre-populated code that allows you to list stream connections and process stream data:

Image of a stream processing template
click to enlarge

You can connect MongoDB for VS Code to only one deployment at a time. To change the active connection to a different deployment, or to connect to a deployment from which you were disconnected:

1
Image VSCode side bar menu.
click to enlarge
2

You can connect the MongoDB Shell or legacy mongo shell to your active deployment.

  • The shell that MongoDB for VS Code uses to connect to your deployment is determined by the Shell setting in your extension settings. You can choose either the MongoDB Shell or the legacy mongo shell.

  • The path to your selected shell must exist in your system's PATH. If it does not exist in your PATH, the operation errors.

To connect the shell to your active deployment:

  1. In the MongoDB for VS Code Connections list, right-click your active deployment.

  2. Select Launch MongoDB Shell.

MongoDB for VS Code opens the Terminal window in VS Code and launches the shell connected to your selected deployment.

1
Image VSCode side bar menu.
click to enlarge
2

Expand the Connections pane in the left navigation if it's collapsed.

Image showing connections pane
click to enlarge
3

From the Connections menu, right-click the connection and select Edit Connection...

4

Click Advanced Connection Options to display the advanced connection form.

Tip

You can also click the Edit Connection String toggle in the top-right of the form to edit the connection string URI directly.

Once you are done editing the connection options click Save & Connect.

You can copy a connection string from an existing Visual Studio Code connection.

1
Image VSCode side bar menu.
click to enlarge
2

Expand the Connections pane in the left navigation if it's collapsed.

Image showing connections pane
click to enlarge
3

From the Connections menu, right-click the connection and select Copy Connection String.

The connection string is copied to your clipboard.

To rename a connection to a deployment in MongoDB for VS Code:

1
Image VSCode side bar menu.
click to enlarge
2
3
  1. In the Command Palette, type a new name for the connection.

  2. Press the Enter or Return key to rename the connection.

MongoDB for VS Code provides two methods to disconnect from a deployment. You can:

  • Disconnect with the Command Palette.

  • Disconnect from the MongoDB view in the Activity Bar.

Disconnecting from a MongoDB instance closes the MongoDB for VS Code connection to the active instance. You must reconnect to interact with data in your deployment again.

MongoDB for VS Code provides two methods to remove a connection to a deployment from Visual Studio Code. You can:

  • Remove a connection from the MongoDB view in the Activity Bar.

  • Remove a connection with the Command Palette.

Important

Removing a connection from Visual Studio Code removes it both from the current workspace and from Visual Studio Code globally.

← Install MongoDB for VS Code