Setup MongoDB Server With Python

Setup MongoDB Atlas, Compass and PyMongo

1. Go MongoDB Atlas and set your server

1) https://www.mongodb.com/cloud/atlas

2) Register

3) Set a free Cluster

4) Add a new user to your cluster: username & password

add new cluster user

5) Once you setup, you may see your cluster like this:

cluster detail

6) Click “connect” to obtain your server link string, it may look like this:

mongodb://analytics:@mflix-shard-00-00-ybpql.mongodb.net:27017,mflix-shard-00-01-ybpql.mongodb.net:27017,mflix-shard-00-02-ybpql.mongodb.net:27017/test?ssl=true&replicaSet=mflix-shard-0&authSource=admin

2. Download MongoDB Shell (it includes with MongoDB Comapass)

1) https://www.mongodb.com/

1) Open you local MongoDB Compass, it requires some arguments to connect

2) Go to your online MongoDB Atlas, click connect, then choose MongoDB Shell connecting, then copy the configuration string

3) Paste the string into MongoDB Compass

4. Add new data to MongoDB Altas

1) Once your MongoDB Compass successfully connects to MongoDB Atlas, you may upload your data by Compass

2) Click Collection -> import data

3) You may also use command line to upload data. This requires configure Environment Variables in System.

1) Get you client string from Atlas, and replace with user password you set in step 1:

For example, my link string is like:

1
2
3
4
5
from pymongo import MongoClient

client = MongoClient("mongodb://analytics:<PASSWORD>@mflix-shard-00-00-ybpql.mongodb.net:27017,mflix-shard-00-01-ybpql.mongodb.net:27017,mflix-shard-00-02-ybpql.mongodb.net:27017/test?ssl=true&replicaSet=mflix-shard-0&authSource=admin")

print(client.mflix)

Relation

relationship