Mongo Client

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

On MongoDB = 3.6, end all server sessions created by this client by sending one or more endSessions commands. Close all sockets in the connection pools and stop the monitor threads. If this instance is used again it will be automatically re-opened and the threads restarted unless auto encryption is enabled. The MongoDB CLI is a modern command line interface that enables you to manage your MongoDB services from the terminal. Install MongoDB CLI. View installation instructions. What You Can Do. Studio 3T, formerly MongoChef, is the robust, professional version of Robo 3T owned by the same parent company, 3T Software Labs. Studio 3T is more than just a MongoDB GUI: it’s also an IDE and client. There is no free version of this software (unless you count Robo 3T), and the Core pricing level starts at $149/year per user. Sep 22, 2015 As of today's version of MongoDB (v2.0.1.27 for MongoDB.Driver), there's no need to close or dispose of connections. The client handles it automatically. From the docs: A MongoClient object will be the root object.

Install the extension

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

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.

To connect to a MongoDB database, select Add Connection and enter the connection details for the database then Connect, the default is a local MongoDB server at mongodb://127.0.0.1:27017. You can also enter a connection string, click the 'connect with a connection string' link and paste the connection string.

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.

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

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

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

MongoDB Commands

Client

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

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.

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.

MongoDB on Azure

You can easily create a MongoDB cluster on Azure for Free with MongoDB Atlas.

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.

Next steps

  • Azure Extensions - The VS Code Marketplace has hundreds of 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.
  • Connect to MongoDB

Author: MongoDB Documentation Team

There are several ways to connect to your MongoDB instance.

  • MongoDB Compass for access through a downloadable user interface
  • mongo interactive shell
  • programmatic access through a number of programming APIs.

The following guide steps you through installing the MongoDB client of your choice.

Time required: 15 minutes

What You’ll Need¶

If you are using MongoDB in the Cloud (Atlas), you will need to:

Or, if you plan on using a local instance of MongoDB, you will need to:

  • Install MongoDB and start up an instance towhich you will connect.

Check Your Environment¶

  • Ensure that your client platform is compatible with MongoDB.Refer to the Supported Platformstable for more information.

Procedure¶

Tip

Select the client and environment you will use at the top of this page.

Install your client¶

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • Motor
    • C#
    • Go

The mongo shell is packaged with the MongoDB ServerCommunity and Enterprise distributions, and is also availablefor users of Atlas as a client-only download.

MongoDB binaries are located in a directory that starts with“mongodb-“. You should see a file named mongo, which isthe shell executable.

If you do not have mongo shell installed, follow theinstall directions for your environment.

If you do not already have a mongo shell, you can downloadjust the shell by logging into Atlas. For your cluster, clickConnect.

Under ChecktheIPWhitelist, add the IP address of theclient you wish to have connecting to Atlas. Then click theConnectwiththeMongoShell. Follow the instructions inthe dialog to download and install the shell.

  • Windows
  • macOS
  • Linux

Download the latest stable version for your environment.

After downloading, click on the downloaded .msi file. TheWindows Installer will guide you through the installation.

Download the latest stable version for your environment.

Double click the tgz file to untar the file.

Download the latest stable version for your environment.

Extract the tar file and locate the mongo executable underthe bin directory of your install root.

Mongo Client

To install Compass, see the Compass installation instructions

To install Pymongo, see the Pymongo documentation.

Client

To install the Java driver, see the Java Driver documentation

To install the node.js driver, see the Node.js Driver documentation

To install the C#/.NET driver, see the C# Driver documentation

The MongoDB Go driver can be installed using goget:

The output of this may look like a warning stating something like:

Mongo

This is expected output.

Alternatively if you are using the dep package manager toinstall the driver,you can install the main mongo package as well as the bson andmongo/options package using this command:

Obtain your MongoDB connection string¶

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • Motor
    • C#

In order to connect to MongoDB, you will need a URI string. A URI (Uniform Resource Identifier) is similar toa URL, and is supplied as a parameter to the mongoshell, Compass, and the MongoDB drivers when connecting to a MongoDBdeployment.

Note

You can create a URI string for your session in MongoDBGuides by copying and pasting your Atlas connection stringin the form below. Once you copy the string here, anyrelevant code examples within the MongoDB Guides will bepopulated with a URI string. If you have not alreadyretrieved your Atlas connection string, seeSet Up Atlas Connectivity guide.

The URI string used in the guides assumes you have set upauthentication for your MongoDB instance, and have created ausername and password for read and write access to a MongoDBdatabase.

If you have already set up a user with thereadWriteAnyDatabase role through the Secure your MongoDB Deploymentguide, you may use that username and password here, alongwith the admin database in your connection string.

Note

The URI form below allows you to create a URI string foryour session in MongoDB Guides by filling out a form. Onceyou fill out the form, any relevant code examples within theMongoDB Guides will be populated with a URI string. You canchange these at any time by repopulating the form.

Important

If your connection string contains $[password], you will needto replace this string with your password. Use cautionwhere you store and enter passwords, particularly when running froma shell or command prompt. Special characters in passwords must beescaped.

Connect to your MongoDB instance¶

Tip

Have you whitelisted your IP address for access to Atlas?For more details see Step 3, WhitelistyourIPAddressandSelectYourConnectionMethod inSet Up Atlas Connectivity.

  • Mongo Shell
  • Compass
  • Python
  • Java (Sync)
  • Node.js
  • Other
    • C#
    • Go

Select the operating system platform on which you are running theMongoDB client you have selected.

  • Windows
  • macOS
  • Linux

Pass the URI to the mongo shell followed by the --passwordoption. You will then be prompted for your password.

Pass the URI to the mongo shell followed by the --passwordoption. You will then be prompted for your password.

Pass the URI to the mongo shell followed by the --passwordoption. You will then be prompted for your password.

If you wish to manually configure your Compass connection, loadCompass and select the NewConnection link. You will see aform where you can enter connection information for MongoDB.

Atlas users can copy a URI string from the Atlasconsole into Compass. MongoDB Compass can detect whether you have a MongoDBURI connection string in your system clipboard and auto-populate the connection dialog from the URI.

See Set Up Atlas Connectivity for information onhow to get the Atlas connection string URI into your copybuffer.

If Compass was already running when you copied the URI string,click the NEW CONNECTION button.

You will be prompted to populate the connection dialog.Click Yes.

You should then populate the password field with theproper password for your MongoDB user in the connection form.

Note

Errors related to connecting through Compass willappear in red at the top of the Connect screen.

It’s a good idea to put your connection code in a class sothat it can be reused.

If your connection_string starts with mongodb+srv, you need to install the dnspython module with

Now add code to call the class you just created.

For the MongoDB java driver 3.7 and beyond, use the MongoClients.create() method.

For legacy drivers (prior to 3.7), use:

The MongoDB.Bson package is used in CRUD operations, soyou’ll import it here.

Replace your password and any parameters surrounded by $[]in the connection string in the code below.

For now, you will use the context.TODO().

Later you’ll configure the context specific to your requirements.

You won’t know if the connection has been successful until youuse the connection. A ping is one way you can test theconnection. This is a full example of a Go connection tomongoDB, including a test ping.

In your Go workspace and project folder, run build.

Now run the binary. For binaries that are not installed, you’llhave to specify the path.

If you’d like to run the resulting binary without specifyinga path, install the binary you just built into your Go workspace.

Now run the code. “yourprojectname” is the name of the projectdirectory that contains the file with your main() function.

For installed binaries use:

For binaries that are not installed, you’ll have to specifythe path.

Mongo Client Js

The default timeout for the Go driver to connect to the databaseis 30 seconds. In the event that you are unable to connect,you will see an error that resembles this:

Summary¶

Congratulations. If you have successfully completed this guide,you have connected to your MongoDB instance. In the next group ofguides, you’ll learn how to create, read, update, and delete datain MongoDB.

What’s Next¶

In the next guide, you’ll learn how to insert data into MongoDB.

See Also¶

For CRUD (Create, Read, Update, Delete) guides:

Mongodb Client Mac

© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.