Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions docs/azure/mongodb.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
---
ContentId: d1187f99-354f-4798-9c19-e432e4ae8572
MetaDescription: Working with MongoDB in Visual Studio Code
DateApproved: 11/1/2022
DateApproved: 11/1/2025
---
# Working with MongoDB

Visual Studio Code has great support for working with [MongoDB](https://www.mongodb.com/what-is-mongodb) databases, whether your own instance or in [Azure with MongoDB Atlas](https://www.mongodb.com/cloud/atlas/azure-mongodb?utm_campaign=marketplace&utm_source=&utm_medium=marketplace). With the [MongoDB for VS Code](https://marketplace.visualstudio.com/items?itemName=mongodb.mongodb-vscode) extension, you can create, manage, and query MongoDB databases from within VS Code.
Visual Studio Code has great support for working with [MongoDB](https://www.mongodb.com/what-is-mongodb) databases, whether your own instance or with [Azure DocumentDB (with MongoDB compatibility)](https://learn.microsoft.com/azure/cosmos-db/mongodb/vcore/introduction). With the [DocumentDB for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-documentdb) extension, you can create, manage, and query MongoDB databases from within VS Code.

## Install the extension

MongoDB support for VS Code is provided by the [MongoDB for VS Code](https://marketplace.visualstudio.com/items?itemName=mongodb.mongodb-vscode) extension. To install the MongoDB for VS Code extension, open the Extensions view by pressing `kb(workbench.view.extensions)` and search for 'MongoDB' to filter the results. Select the **MongoDB for VS Code** extension.
MongoDB support for VS Code is provided by the [DocumentDB for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-documentdb) extension. To install the DocumentDB for VS Code extension, open the Extensions view by pressing `kb(workbench.view.extensions)` and search for 'DocumentDB' to filter the results. Select the **DocumentDB for VS Code** extension.

![Select MongoDB for VS Code](images/mongodb/install-cosmosdb-extension.png)
![TODO Select DocumentDB for VS Code](images/mongodb/install-cosmosdb-extension.png)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khelanmodi Are you planning on updating the screenshot?

Copy link
Author

@khelanmodi khelanmodi Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntrogh i am gonna update all the screenshots next week. i will @ you once the pr is ready for review


## Connect to MongoDB

Once you've installed the MongoDB for VS Code extension, you'll notice there is a new **MongoDB** Activity Bar view. Select the MongoDB view and you'll see the MongoDB Explorer.
Once you've installed the DocumentDB for VS Code extension, you'll notice there is a new **DocumentDB** Activity Bar view. Select the DocumentDB view and you'll see the Explorer.

![MongoDB explorer](images/mongodb/cosmosdb-explorer.png)
![DocumentDB explorer](images/mongodb/cosmosdb-explorer.png)

To connect to a MongoDB database:
To connect to a MongoDB-compatible database:

1. Select **Add Connection** in the MongoDB view
1. Select **Add New Connection** in the DocumentDB Connection view

1. Next, choose to connect with a connection string or use advanced connection options:
1. Next, choose to connect with a connection string or use Service Discovery options:

* Select **Connect with Connection String**, and then enter the connection string in the connection string Quick Pick.
* Select **Connection String**, and then enter the connection string in the connection string Quick Pick.

The default connection string for a local MongoDB is `mongodb://127.0.0.1:27017`.

![Database Connection setup](images/mongodb/attach-database-account-connection-string.png)

* Select **Advanced Connection Settings**, enter the connection details, and then select **Save & Connect**.
* Select **Service Discovery**, choose your provider, and then select **Save & Connect**.

![Database Connection setup](images/mongodb/attach-database-account.png)

>**Note**: Make sure your MongoDB server (mongod.exe) is running if you are connecting to a local MongoDB server.
>**Note**: If you're not already signed in to Azure in VS Code, you'll be prompted to do so. This is required to use Service Discovery.

Once attached, you can work with the MongoDB server, managing MongoDB Databases, Collections, and Documents.
Once connected, you can work with the MongoDB server, managing MongoDB Databases, Collections, and Documents.

![attached MongoDB database](images/mongodb/attached-mongodb-database.png)

Expand All @@ -51,29 +51,26 @@ You can also attach a MongoDB shell to the active connection, simply by right-cl

>**Note**: Make sure the MongoDB shell (`mongo` or `mongosh`) [is installed](https://docs.mongodb.com/mongodb-shell/install#mdb-shell-install) and is on your path. In the extension's settings, you can choose which shell you are using.

## MongoDB Commands
## Using Scrapbook

There are MongoDB specific commands available in the VS Code **Command Palette** (`kb(workbench.action.showCommands)`) as well as through Explorer context menus.
The **DocumentDB Scrapbook** is one of the most powerful features of this extension. It allows you to write, run, and save MongoDB commands directly within a VS Code editor, helping you prototype queries and scripts

![mongodb commands](images/mongodb/mongodb-commands.png)
### Create a New Scrapbook
1. In the DocumentDB Explorer, right-click the desired collection.

## Using Playgrounds

One of the most powerful features of the VS Code MongoDB integration is **Mongo Playgrounds**. Playgrounds let you create, run, and save MongoDB commands from a VS Code editor. Create a new playground with the **MongoDB: Create MongoDB Playground** command.
1. From the menu, select **Create Scrapbook**.

![new mongo Playground](images/mongodb/new-mongo-scrapbook.png)

In a playground, you can reference MongoDB entities and commands and you get rich IntelliSense as you type. Playgrounds are useful for prototyping database operations and queries. Execute selected lines in the playground queries with the **MongoDB: Run Selected Lines From Playground** command.
In a scrapbook, you can reference MongoDB entities and commands and you get rich IntelliSense as you type. Scrapbooks are useful for prototyping database operations and queries. Execute selected lines in the scrapbook queries with **Run Command**.

![mongodb Playground](images/mongodb/scrapbook.png)

![Run Playground queries](images/mongodb/run-playground.png)

## MongoDB on Azure

You can easily create a MongoDB cluster on Azure for **Free** with [MongoDB Atlas](https://www.mongodb.com/cloud/atlas/signup?utm_campaign=marketplace&utm_source=signup&utm_medium=marketplace).
## MongoDB on Azure DocumentDB

Choose **Create a New Cluster** from the dashboard and choose **Azure** as the Cloud Provider. Once the cluster is created, connect to using the connection string provided by **MongoDB Atlas**.
You can easily create a managed MongoDB cluster on Azure for **Free** with [Azure DocumentDB](https://learn.microsoft.com/azure/cosmos-db/mongodb/vcore/quickstart-portal).

![Create Azure Cluster](images/mongodb/create-azure-cluster.png)

Expand Down