Skip to content

Latest commit

 

History

History
90 lines (60 loc) · 2.67 KB

class-3.md

File metadata and controls

90 lines (60 loc) · 2.67 KB

Class 3

To prepare for class, please sign up for a MongoDB Atlas account https://account.mongodb.com/

Lambda Material:

Part I

Recap of previous assignment (inserting "titanic.csv" file data into a PostgreSQL database).

Part II and III

MongoDB Docs:

Vocabulary comparison:

Configuring Mongo Atlas:

  • IP Address to allow connections from anywhere: 0.0.0.0/0

Installing packages:

# for pipenv users:
pipenv install pymongo dnspython

# for conda users:
pip install pymongo dnspython
# app/mongo_queries.py

import pymongo
import os
from dotenv import load_dotenv

load_dotenv()

DB_USER = os.getenv("MONGO_USER", default="OOPS")
DB_PASSWORD = os.getenv("MONGO_PASSWORD", default="OOPS")
CLUSTER_NAME = os.getenv("MONGO_CLUSTER_NAME", default="OOPS")

connection_uri = f"mongodb+srv://{DB_USER}:{DB_PASSWORD}@{CLUSTER_NAME}.mongodb.net/test?retryWrites=true&w=majority"
print("----------------")
print("URI:", connection_uri)

client = pymongo.MongoClient(connection_uri)
print("----------------")
print("CLIENT:", type(client), client)

db = client.test_database # "test_database" or whatever you want to call it
print("----------------")
print("DB:", type(db), db)

collection = db.pokemon_test # "pokemon_test" or whatever you want to call it
print("----------------")
print("COLLECTION:", type(collection), collection)

print("----------------")
print("COLLECTIONS:")
print(db.list_collection_names())

collection.insert_one({
    "name": "Pikachu",
    "level": 30,
    "exp": 76000000000,
    "hp": 400,
})
print("DOCS:", collection.count_documents({}))
print(collection.count_documents({"name": "Pikachu"}))

FYI: if you see "pymongo.errors.ServerSelectionTimeoutError [SSL: CERTIFICATE_VERIFY_FAILED]", and you have already allowed access from all IP addresses, and you have already installed the dnspython package, try adding &ssl=true&ssl_cert_reqs=CERT_NONE to the end of the connection string (thanks to Aaron from DS 14)!

Reference also "app/mongo_prep.py".