Connecting to MongoDB via MongoDB Shell/ Mongo URI

Have you ever been fed up with the complicated procedures connecting to MongoDB? In Mongo Clusters, everything is simple and smooth. The following article will help you obtain the connection method easily.

Connect to MongoDB via MongoDB Shell

1. Locate the target MongoDB deployment

Log in to the Control Panel and locate your target deployment.

2. Create the database and user

Click on “Databases and Users” tab in the menu bar. If there is not any database or user, create the database and user first. Just a warm reminder, please associate one user at least with the database before the connection.

3. Find MongoDB Shell Connection String

Click on "Details" of the specific database, and you will enter the detail page with MongoDB shell connection string.

4. Connect to MongoDB Server

Download and install MongoDB community server program. Copy the Shell connection string and replace the <dbuser>, <dbpassword> with your own database user and password, and then paste it onto command window to connect to MongoDB server.

Connect to MongoDB via Mongo URI

1. Locate the target MongoDB deployment

Log in to the Control Panel and locate your target deployment.

2. Create the database and user

Click on “Databases and Users” tab in the menu bar. If there is not any database or user, create the database and user first. Just a warm reminder, please associate one user at least with the database before the connection.

3. Find MongoDB URI

Click on "Details" of the specific database, and you will enter the detail page with MongoDB URI.

4. Install PyMongo

Install PyMongo and then use the URI in the program. Please find examples below:

Related links

Install PyMongo

pip install pymongo

Python Demo Code

import pymongo
import random

## input the password
dbuser = "XXX"
dbpassword = "XXX"
database_name = "XXX"
uri = 'mongodb://<dbuser>:<dbpassword>@XXXX.cloudclusters.io/<database_name>?authSource=admin'.format(dbuser,dbpassword)
## Get mongo client
client = pymongo.MongoClient(uri)
db = client.database_name
element_num = 10
for id in range(element_num):
    name = random.choice(['dog','cat','bird','fish'])
    ## Insert random data
    db.info.insert_one({'id':id, 'name':name})
## Query the inserted data
content = db.info.find()
for i in content:
    print(i)
Copyright © 2019 Cloud Clusters Inc.Revised on 01/09/2019

results matching ""

    No results matching ""