MongoDB for VS Code is currently available as a Preview in the Visual StudioMarketplace. The product, its features, and the correspondingdocumentation may change during the Preview stage.
MongoDB for VS Code Extension allows you to connect to your MongoDB instance and enables you to interact in a way that fits into your native workflow and development tools. You can navigate and browse your MongoDB databases and collections, and prototype queries and aggregations for use in your applications. Navigate your MongoDB Data. Visual Studio; Visual Studio Code; Visual Studio for Mac; Go to File New Project. Select the ASP.NET Core Web Application project type, and select Next. Name the project BooksApi, and select Create. Select the.NET Core target framework and ASP.NET Core 3.0. Select the API project template, and select Create. Npm install npm run local-install. This will compile and package MongoDB for VS Code into a.vsix file and add the extension to your VS Code. To install this locally on windows: npm install.nodemodules.binvsce.cmd package code -install-extension./mongodb-vscode-.vsix. You can execute the javascript code via mongo shell in Visual Studio code. Write all of your javascript code that connects with mongodb and defines all your function and save it as mongoScript.js.
This page outlines how to use MongoDB for VS Code to connect to aMongoDB host. You can connect to a standalone, replica set,or sharded cluster host.
If you need to create a MongoDB host, consider usingMongoDB Atlas.Atlas is a cloud-hosted database-as-a-service which requires noinstallation, offers a free tier to get started, and provides acopyable URI to easily connect MongoDB for VS Code to your deployment.
To create a cluster on the Atlas free tier, run the commandMongoDB: Open Overview Page
in the Visual Studio Code CommandPalette and then click Create free cluster.
To create an Atlas cluster using a Terraform template includedwith 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.
Considerations¶
When connecting MongoDB for VS Code to a replica set, use either thereplica set SRV record or Replica Set Namewhen filling in your connection information. It is not recommended toconnect directly to an individual replica set member.
- If the member to which you are connected switches from aprimary member to asecondary or vice versa asthe result of an election, MongoDB for VS Code may either forcibly closethe 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 valueofmongodb-vscode <version>
for all deployment connections.
Create a Connection to a Deployment¶
MongoDB for VS Code provides two methods to create a connection to a deployment. Youcan:
- 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 adeployment's details. Use this option if Atlas hosts yourdeployment or if you have a connection string for a deploymentavailable.
Define how your connection is saved with theDefault Connection Saving Locationsetting:
Setting | Description |
---|---|
Global | Save your connection globally in VS Code, so it can be accessedfrom any workspace. |
Workspace | Save your connection in your workspace. You cannotaccess the connection from a different workspace. |
Session | Save the connection for only this VS Code session. The connectionis lost when you close VS Code. |
When you provide a connection string, MongoDB for VS Code supportsmost Connection String Optionssupported by MongoDB.
Obtain your connection string.¶
You can use either the Standard Connection String Formator the DNS Seedlist Connection Format.
To obtain the connection string for an Atlas cluster:
- Navigate to your AtlasClusters view.
- Click Connect for your desired cluster.
- Click Connect with MongoDB Compass.
- Copy the provided connection string.
- To learn how to format the connection string for a deploymentwhich is not hosted on Atlas, seeConnection String URI Format.
Open the Visual Studio Code Command Palette.¶
Open the Visual Studio Code Command Palette to paste your connectionstring and connect to your cluster. You can open the CommandPalette in one of the following ways:
Perform the action described in the following table for youroperating system and method:
Operating System Method Actions Any Visual Studio Code Menu View > Command Palette MacOS Keyboard Shortcut Press Command
+Shift
+P
Windows and Linux Keyboard Shortcut Press Control
+Shift
+P
From the Command Palette, select MongoDB: OpenOverview Page.
Enter
mongodb
in the Command Palette to display all of theMongoDB for VS Code commands you can use.- In the Overview page, click Connect under theConnect with Connection String.
Paste your connection string into the Command Palette.¶
If your deployment requires authentication, ensure that you updatethe connection string with credentials of a database user for yourdeployment. The connection might succeed without these credentials,but you won't be able to interact with data in your deployment.
Press the Enter or Return key.¶
Activate a Connection¶
You can connect MongoDB for VS Code to only one deployment at a time. To change theactive connection to a different deployment, or to connect to adeployment from which you were disconnected:
In Visual Studio Code, click the MongoDB view in the Activity Bar.¶
Right-click the connection you want to activate, then click Connect.¶
Launch MongoDB Shell¶
You can connect theMongoDB Shell or legacymongo
shell to your active deployment.
Considerations¶
- The shell that MongoDB for VS Code uses to connect to your deployment isdetermined by the Shell setting in yourextension settings. You can choose eitherthe MongoDB Shell orlegacy
mongo
shell. - The path to your selected shell must exist in your system's
PATH
.If it does not exist in yourPATH
, the operation errors.
Procedure¶
To connect the shell to your active deployment:
- In the MongoDB for VS CodeConnections list, right-click your activedeployment.
- Select Launch MongoDB Shell.
MongoDB for VS Code opens the Terminal window in VS Code and launchesthe shell connected to your selected deployment.
Rename a Connection¶
To rename a connection to a deployment in MongoDB for VS Code:
In Visual Studio Code, click the MongoDB view in the Activity Bar.¶
Right-click the connection you want to activate, then click Rename Connection.¶
Rename the connection.¶
- In the Command Palette, type a new name for the connection.
- Press the Enter or Return key to rename the connection.
Disconnect from a Deployment¶
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 theActivity Bar.
In Visual Studio Code, open the Command Palette.¶
Operating System | Method | Actions |
---|---|---|
Any | Visual Studio Code Menu | View > Command Palette |
MacOS | Keyboard Shortcut | Press Command + Shift + P |
Windows and Linux | Keyboard Shortcut | Press Control + Shift + P |
Disconnect.¶
From the Command Palette, selectMongoDB: Disconnect.
Enter
mongodb
in the Command Palette to display all of theMongoDB for VS Code commands you can use.- Press Enter.
Disconnecting from a MongoDB instance closes the MongoDB for VS Code connection tothe active instance. You must reconnect tointeract with data in your deployment again.
Remove a Connection¶
MongoDB for VS Code provides two methods to remove a connection to a deployment fromVisual Studio Code. You can:
- Remove a connection with the Command Palette, or
- Remove a connection from the MongoDB view in theActivity Bar.
Removing a connection from Visual Studio Code removes it both from thecurrent workspace and from Visual Studio Code globally.
Mongodb With Visual Studio Code
In Visual Studio Code, open the Command Palette.¶
Operating System | Method | Actions |
---|---|---|
Any | Visual Studio Code Menu | View > Command Palette |
MacOS | Keyboard Shortcut | Press Command + Shift + P |
Windows and Linux | Keyboard Shortcut | Press Control + Shift + P |
From the Command Palette, select MongoDB: Remove Connection...¶
Enter mongodb
in the Command Palette to display all of theMongoDB for VS Code commands you can use.
Mongodb In Visual Studio Code
If you have more than one connection, select the connection you wantto remove from the Command Palette.
Click Yes to remove the connection.¶
© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
MongoDB for VS Code is currently available as a Preview in the Visual StudioMarketplace. The product, its features, and the correspondingdocumentation may change during the Preview stage.
MongoDB provides an extension for Microsoft Visual Studio Code which lets you work withMongoDB and your data directly within your coding environment. Make a zip file in mac.
Features¶
You can use MongoDB for Visual Studio Code to:
- Explore your MongoDB Data.
- Prototype queries and run MongoDB commands.
- Create a Shared Tier Atlas cluster using a Terraform template.
Get Started¶
- Install MongoDB for VS Code
- Install MongoDB for VS Code from the VS Code Marketplace.
- Connect to Your MongoDB Deployment
- Connect to your deployment using MongoDB for VS Code.
- Navigate Your Data
- Explore your databases, collections, and documents.
- Explore Your Data with Playgrounds
- Use JavaScript environments to interact your data. Prototype queries,run aggregations, and more.
- Create an Atlas Cluster from a Template using Terraform
- Create a Shared Tier Atlas cluster using a Terraform template included with MongoDB for VS Code.
© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.