Lecture 3: MongoDB Atlas and connecting to MongoDB database#
Learning objectives#
By the end of this lecture, students should be able to:
Create and configure a MongoDB cluster on MongoDB Atlas to manage databases and collections in a cloud-based environment.
Establish a secure connection to their MongoDB Atlas cluster using PyMongo and perform basic database operations through Python scripts.
Slides#
Note
Download a PDF version here
Supplemental materials#
Quick start on MongoDB Atlas#
Demo of connecting to a MongDB via pymongo#
Warning
You would need to create a separate json file to securely store your MongoDB username and password.
Make sure these information are not uploaded to any public server, that means DO NOT commit this credentials_mongodb.json
file. I have disabled the ability to commit this file by default in the .gitignore settings.
The json file should follow the template below:
{
"host": "<your_host>",
"port": 27017,
"username": "<your_username>",
"password": "<your_password>"
}
If you forgot how to retrieve your host URI, please see this tutorial
Below is the starter code to connect to your MongoDB database, provided that you have created a credentials_mongodb.json
file with the correct information.
from pymongo import MongoClient # import mongo client to connect
import json # import json to load credentials
import urllib.parse
# load credentials from json file
with open('credentials_mongodb.json') as f:
login = json.load(f)
# assign credentials to variables
username = login['username']
password = urllib.parse.quote(login['password'])
host = login['host']
url = "mongodb+srv://{}:{}@{}/?retryWrites=true&w=majority".format(username, password, host)
# connect to the database
client = MongoClient(url)
List all db names collections
# list all databases
client.list_database_names()
['sample_airbnb',
'sample_analytics',
'sample_geospatial',
'sample_guides',
'sample_mflix',
'sample_restaurants',
'sample_supplies',
'sample_training',
'sample_weatherdata',
'admin',
'local']
# list all collections in the sample_mflix database
client.sample_mflix.list_collection_names()
['comments', 'sessions', 'movies', 'theaters', 'users', 'embedded_movies']
# show the first document
client.sample_mflix.movies.find_one()
{'_id': ObjectId('573a1390f29313caabcd50e5'),
'plot': 'The cartoonist, Winsor McCay, brings the Dinosaurus back to life in the figure of his latest creation, Gertie the Dinosaur.',
'genres': ['Animation', 'Short', 'Comedy'],
'runtime': 12,
'cast': ['Winsor McCay', 'George McManus', 'Roy L. McCardell'],
'num_mflix_comments': 0,
'poster': 'https://m.media-amazon.com/images/M/MV5BMTQxNzI4ODQ3NF5BMl5BanBnXkFtZTgwNzY5NzMwMjE@._V1_SY1000_SX677_AL_.jpg',
'title': 'Gertie the Dinosaur',
'fullplot': 'Winsor Z. McCay bets another cartoonist that he can animate a dinosaur. So he draws a big friendly herbivore called Gertie. Then he get into his own picture. Gertie walks through the picture, eats a tree, meets her creator, and takes him carefully on her back for a ride.',
'languages': ['English'],
'released': datetime.datetime(1914, 9, 15, 0, 0),
'directors': ['Winsor McCay'],
'writers': ['Winsor McCay'],
'awards': {'wins': 1, 'nominations': 0, 'text': '1 win.'},
'lastupdated': '2015-08-18 01:03:15.313000000',
'year': 1914,
'imdb': {'rating': 7.3, 'votes': 1837, 'id': 4008},
'countries': ['USA'],
'type': 'movie',
'tomatoes': {'viewer': {'rating': 3.7, 'numReviews': 29},
'lastUpdated': datetime.datetime(2015, 8, 10, 19, 20, 3)}}
# list all keys in the first document
keys_all = client.sample_mflix.movies.find_one().keys()
keys_all
dict_keys(['_id', 'plot', 'genres', 'runtime', 'cast', 'num_mflix_comments', 'poster', 'title', 'fullplot', 'languages', 'released', 'directors', 'writers', 'awards', 'lastupdated', 'year', 'imdb', 'countries', 'type', 'tomatoes'])