Navigation

    Connect to Your MongoDB Deployment

    Info With Circle IconCreated with Sketch.Note
    Preview

    MongoDB for VS Code is currently available as a Preview in the Visual Studio Marketplace. The product, its features, and the corresponding documentation may change during the Preview stage.

    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.

    Info With Circle IconCreated with Sketch.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 methods to create a connection to a deployment. You can:

    • Provide a deployment connection string, or
    • Fill in a deployment information in specific fields.

    Providing a connection string is faster and easier than filling in a deployment's details. Use this option if Atlas hosts your deployment or if you have a connection string for a deployment available.

    Define how your connection is saved with the Default Connection Saving Location setting:

    SettingDescription
    GlobalSave your connection globally in VS Code, so it can be accessed from any workspace.
    WorkspaceSave your connection in your workspace. You cannot access the connection from a different workspace.
    SessionSave the connection for only this VS Code session. The connection is lost when you close VS Code.

    When you provide a connection string, MongoDB for VS Code supports most Connection String Options supported by MongoDB.

    1

    You can use either the Standard Connection String Format or the DNS Seedlist Connection Format.

    • To obtain the connection string for an Atlas cluster:

      1. Navigate to your Atlas Clusters view.
      2. Click Connect for your desired cluster.
      3. Click Connect with MongoDB Compass.
      4. Copy the provided connection string.
    • To learn how to format the connection string for a deployment which is not hosted on Atlas, see Connection String URI Format.
    2

    Open the Visual Studio Code Command Palette to paste your connection string and connect to your cluster. You can open the Command Palette in one of the following ways:

    1. Perform the action described in the following table for your operating system and method:

      Operating SystemMethodActions
      AnyVisual Studio Code MenuView > Command Palette
      MacOSKeyboard ShortcutPress Command + Shift + P
      Windows and LinuxKeyboard ShortcutPress Control + Shift + P
    2. From the Command Palette, select MongoDB: Open Overview Page.

      Bulb IconTip

      Enter mongodb in the Command Palette to display all of the MongoDB for VS Code commands you can use.

    3. In the Overview page, click Connect under the Connect with Connection String.
    3
    Important With Circle IconCreated with Sketch.Important

    If your deployment requires authentication, ensure that you update the connection string with credentials of a database user for your deployment. The connection might succeed without these credentials, but you won't be able to interact with data in your deployment.

    4

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

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

    1
    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, or
    • Disconnect from the MongoDB view in the Activity Bar.
    1
    Operating SystemMethodActions
    AnyVisual Studio Code MenuView > Command Palette
    MacOSKeyboard ShortcutPress Command + Shift + P
    Windows and LinuxKeyboard ShortcutPress Control + Shift + P
    2
    1. From the Command Palette, select MongoDB: Disconnect.

      Bulb IconTip

      Enter mongodb in the Command Palette to display all of the MongoDB for VS Code commands you can use.

    2. Press Enter.

    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 with the Command Palette, or
    • Remove a connection from the MongoDB view in the Activity Bar.
    Important With Circle IconCreated with Sketch.Important

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

    1
    Operating SystemMethodActions
    AnyVisual Studio Code MenuView > Command Palette
    MacOSKeyboard ShortcutPress Command + Shift + P
    Windows and LinuxKeyboard ShortcutPress Control + Shift + P
    2
    Bulb IconTip

    Enter mongodb in the Command Palette to display all of the MongoDB for VS Code commands you can use.

    If you have more than one connection, select the connection you want to remove from the Command Palette.

    3
    Give Feedback