Movatterモバイル変換


[0]ホーム

URL:


TryMCP servers to extend agent mode in VS Code!

Dismiss this update

Working with MongoDB

Visual Studio Code has great support for working withMongoDB databases, whether your own instance or inAzure with MongoDB Atlas. With theMongoDB for VS Code extension, you can create, manage, and query MongoDB databases from within VS Code.

Install the extension

MongoDB support for VS Code is provided by theMongoDB for VS Code extension. To install the MongoDB for VS Code extension, open the Extensions view by pressing⇧⌘X (Windows, LinuxCtrl+Shift+X) and search for 'MongoDB' to filter the results. Select theMongoDB for VS Code extension.

Select MongoDB for VS Code

Connect to MongoDB

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

MongoDB explorer

To connect to a MongoDB database:

  1. SelectAdd Connection in the MongoDB view

  2. Next, choose to connect with a connection string or use advanced connection options:

    • SelectConnect with Connection String, and then enter the connection string in the connection string Quick Pick.

      The default connection string for a local MongoDB ismongodb://127.0.0.1:27017.

      Database Connection setup

    • SelectAdvanced Connection Settings, enter the connection details, and then selectSave & Connect.

      Database Connection setup

Note: Make sure your MongoDB server (mongod.exe) is running if you are connecting to a local MongoDB server.

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

attached MongoDB database

You can expand databases to view their collections with their schema and indexes and you can select individual MongoDB Documents to view their JSON.

open mongodb document

You can also attach a MongoDB shell to the active connection, simply by right-clicking on the connection itself.

MongoDB Connection

Note: Make sure the MongoDB shell (mongo ormongosh)is installed and is on your path. In the extension's settings, you can choose which shell you are using.

MongoDB Commands

There are MongoDB specific commands available in the VS CodeCommand Palette (⇧⌘P (Windows, LinuxCtrl+Shift+P)) as well as through Explorer context menus.

mongodb commands

Using Playgrounds

One of the most powerful features of the VS Code MongoDB integration isMongo Playgrounds. Playgrounds let you create, run, and save MongoDB commands from a VS Code editor. Create a new playground with theMongoDB: Create MongoDB Playground command.

new mongo Playground

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 theMongoDB: Run Selected Lines From Playground command.

mongodb Playground

Run Playground queries

MongoDB on Azure

You can easily create a MongoDB cluster on Azure forFree withMongoDB Atlas.

ChooseCreate a New Cluster from the dashboard and chooseAzure as the Cloud Provider. Once the cluster is created, connect to using the connection string provided byMongoDB Atlas.

Create Azure Cluster

Next steps

  • Azure Extensions - The Visual Studio Marketplace has hundreds of VS Code extensions for Azure and the cloud.
  • Deploying to Azure - Learn step-by-step how to deploy your application to Azure.
  • Working with Docker - Put your application in a Docker container for easy reuse and deployment.
11/1/2022

[8]ページ先頭

©2009-2025 Movatter.jp