|
1 | 1 | --- |
2 | 2 | ContentId: d1187f99-354f-4798-9c19-e432e4ae8572 |
3 | 3 | MetaDescription: Working with MongoDB in Visual Studio Code |
4 | | -DateApproved: 11/1/2022 |
| 4 | +DateApproved: 11/1/2025 |
5 | 5 | --- |
6 | 6 | # Working with MongoDB |
7 | 7 |
|
8 | | -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. |
| 8 | +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. |
9 | 9 |
|
10 | 10 | ## Install the extension |
11 | 11 |
|
12 | | -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. |
| 12 | +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. |
13 | 13 |
|
14 | | - |
| 14 | + |
15 | 15 |
|
16 | 16 | ## Connect to MongoDB |
17 | 17 |
|
18 | | -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. |
| 18 | +Once you've installed the DocumentDB for VS Code extension, you'll notice there is a new **DocumentDB** logo in the Activity Bar view. Select the DocumentDB logo and you'll see the Explorer. |
19 | 19 |
|
20 | | - |
| 20 | + |
21 | 21 |
|
22 | | -To connect to a MongoDB database: |
| 22 | +To connect to a MongoDB-compatible database: |
23 | 23 |
|
24 | | -1. Select **Add Connection** in the MongoDB view |
| 24 | +1. Select **Add New Connection** in the DocumentDB Connection view |
25 | 25 |
|
26 | | -1. Next, choose to connect with a connection string or use advanced connection options: |
| 26 | +1. Next, choose to connect with a connection string or use Service Discovery options: |
27 | 27 |
|
28 | | - * Select **Connect with Connection String**, and then enter the connection string in the connection string Quick Pick. |
| 28 | + * Select **Connection String**, and then enter the connection string in the connection string Quick Pick. |
29 | 29 |
|
30 | | - The default connection string for a local MongoDB is `mongodb://127.0.0.1:27017`. |
| 30 | +  |
31 | 31 |
|
32 | | -  |
| 32 | + * Select **Service Discovery**, choose your provider, and then select **Save & Connect**. |
33 | 33 |
|
34 | | - * Select **Advanced Connection Settings**, enter the connection details, and then select **Save & Connect**. |
| 34 | +  |
35 | 35 |
|
36 | | -  |
| 36 | +>**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. |
37 | 37 |
|
38 | | ->**Note**: Make sure your MongoDB server (mongod.exe) is running if you are connecting to a local MongoDB server. |
| 38 | +Once connected, you can work with the MongoDB server, managing MongoDB Databases, Collections, and Documents. |
39 | 39 |
|
40 | | -Once attached, you can work with the MongoDB server, managing MongoDB Databases, Collections, and Documents. |
| 40 | +You can expand databases to view their collections in JSON/ Table/ Tree view with their schema and indexes. |
41 | 41 |
|
42 | | - |
| 42 | + |
43 | 43 |
|
44 | | -You can expand databases to view their collections with their schema and indexes and you can select individual MongoDB Documents to view their JSON. |
| 44 | +You can also attach a MongoDB shell to the active connection, simply by right-clicking on the connection itself and selecting **Launch Shell**. |
45 | 45 |
|
46 | | - |
| 46 | + |
47 | 47 |
|
48 | | -You can also attach a MongoDB shell to the active connection, simply by right-clicking on the connection itself. |
| 48 | +>**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. |
49 | 49 |
|
50 | | - |
| 50 | +## Using Scrapbook |
51 | 51 |
|
52 | | ->**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. |
| 52 | +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 |
53 | 53 |
|
54 | | -## MongoDB Commands |
| 54 | +### Create a New Scrapbook |
| 55 | +1. In the DocumentDB Explorer, right-click the desired collection. |
55 | 56 |
|
56 | | -There are MongoDB specific commands available in the VS Code **Command Palette** (`kb(workbench.action.showCommands)`) as well as through Explorer context menus. |
| 57 | +1. From the menu, click **DocumentDB Scrapbook** and select **New DocumentDB Scrapbook**. |
57 | 58 |
|
58 | | - |
| 59 | + |
59 | 60 |
|
60 | | -## Using Playgrounds |
| 61 | +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**. |
61 | 62 |
|
62 | | -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. |
| 63 | + |
63 | 64 |
|
64 | | - |
| 65 | +## MongoDB on Azure DocumentDB |
65 | 66 |
|
66 | | -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. |
67 | | - |
68 | | - |
69 | | - |
70 | | - |
71 | | - |
72 | | -## MongoDB on Azure |
73 | | - |
74 | | -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). |
75 | | - |
76 | | -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**. |
77 | | - |
78 | | - |
| 67 | +You can easily create a managed MongoDB cluster on Azure for **Free** with [Azure DocumentDB](https://aka.ms/documentdb). |
79 | 68 |
|
80 | 69 | ## Next steps |
81 | 70 |
|
| 71 | +* [Index Advisor](https://learn.microsoft.com/azure/documentdb/index-advisor) - Learn how to optimize your MongoDB performance with Index Advisor. |
82 | 72 | * [Azure Extensions](/docs/azure/extensions.md) - The Visual Studio Marketplace has hundreds of VS Code extensions for Azure and the cloud. |
83 | 73 | * [Deploying to Azure](/docs/azure/deployment.md) - Learn step-by-step how to deploy your application to Azure. |
84 | 74 | * [Working with Docker](/docs/azure/docker.md) - Put your application in a Docker container for easy reuse and deployment. |
0 commit comments