Connecting to MongoDB via MongoDB Shell/ Mongo URI

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

Connect to MongoDB via MongoDB Shell

1. Locate the target MongoDB deployment

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

2. Create the database and user

Click on “DB & Users” tab in the menu bar. If there is not any database or user, create a database and a 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 “Overview” tab in the menu bar. Scroll down the Overview page and you will see the 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 application.

2. Create the database and user

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

3. Find MongoDB URI

Click on “Overview” tab in the menu bar. Scroll down the Overview page and you will see the MongoDB URI information.

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 09/25/2019

results matching ""

    No results matching ""