Connect to MongoDB

Deployment Type:
Client:

    Author: MongoDB Documentation Team

    There are several ways to connect to your MongoDB instance.

    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:

    Check Your Environment

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

    Procedure

    Tip

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

    1

    Install your client

    The mongo shell is packaged with the MongoDB Server Community and Enterprise distributions, and is also available for 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 is the shell executable.

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

    If you do not already have a mongo shell, you can download just the shell by logging into Atlas. For your cluster, click Connect.

    ../../_images/connect_panel.png

    Under Check the IP Whitelist, add the IP address of the client you wish to have connecting to Atlas. Then click the Connect with the Mongo Shell. Follow the instructions in the dialog to download and install the shell.

    ../../_images/whitelistpanel.png

    Download the latest stable version for your environment.

    After downloading, click on the downloaded .msi file. The Windows 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 under the bin directory of your install root.

    tar -xvzf <tgz file>
    

    To install Compass, see the Compass installation instructions

    To install Pymongo, see the Pymongo documentation.

    To install the Java driver, see the Java Driver documentation

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

    To install Motor, see the Motor documentation

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

    2

    Obtain your MongoDB connection string

    Tip

    If you are using Compass, skip this step.

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

    Note

    You can create a URI string for your session in MongoDB Guides by copying and pasting your Atlas connection string in the form below. Once you copy the string here, any relevant code examples within the MongoDB Guides will be populated with a URI string. If you have not already retrieved your Atlas connection string, see Set Up Atlas Connectivity guide.

    Note

    The URI form below allows you to create a URI string for your session in MongoDB Guides by filling out a form. Once you fill out the form, any relevant code examples within the MongoDB Guides will be populated with a URI string. You can change these at any time by repopulating the form.

    Important

    If your connection string contains $[password], you will need to replace this string with your password. Use caution where you store and enter passwords, particularly when running from a shell or command prompt. Special characters in passwords must be escaped.

    Server deployment type
    • local MongoDB
    • local MongoDB with replica set
    Servers
    3

    Connect to your MongoDB instance

    Tip

    Have you whitelisted your IP address for access to Atlas? For more details see Step 3, Whitelist your IP Address and Select Your Connection Method in Set Up Atlas Connectivity.

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

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

    mongo.exe <URISTRING_SHELL_NOUSER>
    

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

    mongo <URISTRING_SHELL_NOUSER>
    

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

    mongo <URISTRING_SHELL_NOUSER>
    

    If you wish to manually configure your Compass connection, load Compass and select the New Connection link. You will see a form where you can enter connection information for MongoDB.

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

    See Set Up Atlas Connectivity for information on how to get the Atlas connection string URI into your copy buffer.

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

    ../../_images/connect-to-host.png

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

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

    Note

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

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

    from pymongo import MongoClient
    
    class Connect(object):
        @staticmethod    
        def get_connection():
            return MongoClient("<URISTRING>")
    

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

    python -m pip install dnspython
    

    Now add code to call the class you just created.

    from connect import Connect
    from pymongo import MongoClient
    
    connection = Connect.get_connection()
    
        final String uriString = "<URISTRING>";
        MongoClientURI uri = new MongoClientURI(uriString);
        MongoClient mongoClient = new MongoClient(uri);
    
    const MongoClient = require('mongodb').MongoClient;
    const assert = require('assert');
    
    // Connection URL
    const url = '<URISTRING>';
    
    // Use connect method to connect to the Server
    MongoClient.connect(url, function(err, client) {
      assert.equal(null, err);
      client.close();
    });
    

    The asyncio and pprint imports will be used as you add functionality to your example code.

    import motor.motor_asyncio
    import asyncio
    import pprint
    
    client = motor.motor_asyncio.AsyncIOMotorClient('<URISTRING>')
    

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

    using System;
    using MongoDB.Bson;
    using MongoDB.Driver;
    
    namespace csharptest
    {
        class Connect
        {
            static void Main(string[] args)
            {
               var client = new MongoClient("<URISTRING>");
            }
        }
    }
    

    Summary

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

    What’s Next

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