An image showing an example product table forms a relational database (Image by author)
A set of documents that contain information (Image by author)
pip install pymongo
import pymongo
client = pymongo.MongoClient(“mongodb://localhost:27017/”)
db = client[“med_data”]
my_collection = db["patient_data"]
patient_record = {
"Name": "Maureen Skinner",
"Age": 87,
"Sex": "F",
"Blood pressure": [{"sys": 156}, {"dia": 82}],
"Heart rate": 82
}
my_collection.insert_one(patient_record)
for item in my_collection.find():
print(item)
Image by the author
from pprint import pprintfor item in my_collection.find():
pprint(item)
Image by the author
patient_records = [
{
"Name": "Adam Blythe",
"Age": 55,
"Sex": "M",
"Blood pressure": [{"sys": 132}, {"dia": 73}],
"Heart rate": 73
},
{
"Name": "Darren Sanders",
"Age": 34,
"Sex": "M",
"Blood pressure": [{"sys": 120}, {"dia": 70}],
"Heart rate": 67
},
{
"Name": "Sally-Ann Joyce",
"Age": 19,
"Sex": "F",
"Blood pressure": [{"sys": 121}, {"dia": 72}],
"Heart rate": 67
}
]
my_collection.insert_many(patient_records)
my_collection.update_one({"Name": "Darren Sanders"}, {"$set":{"Heart rate": 88}})
patient_record = {
"Hospital number": "3432543",
"Name": "Karen Baker",
"Age": 45,
"Sex": "F",
"Blood pressure": [{"sys": 126}, {"dia": 72}],
"Heart rate": 78,
"Test results" : []
}
patient_record = {
"Hospital number": "3432543",
"Name": "Karen Baker",
"Age": 45,
"Sex": "F",
"Blood pressure": [{"sys": 126}, {"dia": 72}],
"Heart rate": 78,
"Test results" : [
{
"ECG": "scansECGsecg00023.png"
},
{
"BIOCHEM": []
}
]
}
patient_record = {
"Hospital number": "3432543",
"Name": "Karen Baker",
"Age": 45,
"Sex": "F",
"Blood pressure": [{"sys": 126}, {"dia": 72}],
"Heart rate": 78,
"Test results" : [
{
"ECG": "scansECGsecg00023.png"
},
{
"BIOCHEM": [{"AST": 37}, {"CK": 180}, {"TROPT": 0.03}]
}
]
}
medication_data = [
{
"_id": ObjectId('60a3e4e5f463204490f70900'),
"Drug name": "Omeprazole",
"Type": "Proton pump inhibitor",
"Oral dose": "20mg once daily",
"IV dose": "40mg",
"Net price (GBP)": 4.29
},
{
"_id": ObjectId('60a3e4e5f463204490f70901'),
"Drug name": "Amitriptyline",
"Type": "Tricyclic antidepressant",
"Oral dose": "30–75mg daily",
"IV dose": "N/A",
"Net price (GBP)": 1.32
}
]
from bson.dbref import DBRefpatient_records = [
{
"Hospital number": "9956734",
"Name": "Adam Blythe",
"Age": 55,
"Sex": "M",
"Prescribed medications": [
DBRef("medication_data", "60a3e4e5f463204490f70900"),
DBRef("medication_data", "60a3e4e5f463204490f70901")
]
},
{
"Hospital number": "4543673",
"Name": "Darren Sanders",
"Age": 34,
"Sex": "M",
"Prescribed medications": [
DBRef("diagnosis_data", "60a3e4e5f463204490f70901")
]
}
]
collection.find({ <query> }, { <field(s)> })
pprint(my_collection.find({"Name": "Darren Sanders"})[0]
query = {"Name": "Darren Sanders"}doc = my_collection.find(query)
for i in doc:
pprint(i)
my_collection.find_one({"Name": "Darren Sanders"})
for heart_rate in my_collection.find({"Heart rate": {"$gt": 70}}, {"Name"}):
pprint(heart_rate)
(Image by author)
result = my_collection.find({
"$and" : [
{
"Heart rate": {"$lte": 70}
},
{
"Age": {"$gt": 20}
}
]
})
for pt in result:
pprint(pt)
(Image by author)
"Blood pressure": [{"sys": 126}, {"dia": 72}]
for normal in my_collection.find({"Blood pressure.sys": {"$lt": 140}}):
pprint(normal)
import jsonwith open('data_file.json') as f:
file_data = json.load(f)

my_collection.insert_many(file_data)

for item in my_collection.find().limit(10):
pprint(item)
my_collection.count_documents({})
(Image by author)
result = my_collection.aggregate(
[
{"$unwind": "$grades"},
{"$match”: {}},
{"$group": {"_id": "$name", "Avg grade": {"$avg": "$grades.score"}}}
]
)
{'Avg grade': 15.2, '_id': 'Red Star Restaurant'}
{'Avg grade': 13.0, '_id': 'Weather Up'}
{'Avg grade': 9.4, '_id': 'La Nueva Playitas'}
{'Avg grade': 13.0, '_id': “Marcella’S Pizzeria & Catering”}
{'Avg grade': 9.0, '_id': 'Hot Wok'}
{'Avg grade': 9.333333333333334, '_id': '99 Favor Taste'}
{'Avg grade': 18.0, '_id': 'Flavors Corner'}
{'Avg grade': 10.666666666666666, '_id': 'Corona Restaurant'}
{'Avg grade': 9.0, '_id': 'Mila Cafe'}
{'Avg grade': 8.0, '_id': 'Circle Line Manhattan'}
{'Avg grade': 15.6, '_id': “The Old Time Vincent’S”}
{'Avg grade': 10.833333333333334, '_id': 'Riko'}
{'Avg grade': 10.0, '_id': 'Fresh Tortillas'}
{'Avg grade': 10.333333333333334, '_id': 'Le Village'}
{'Avg grade': 13.2, '_id': 'Ruay Thai Restaurant'}
{'Avg grade': 12.0, '_id': 'Lechonera Don Pancholo'}
{'Avg grade': 11.0, '_id': 'Pepe Rosso Social'}
. . .
result = my_collection.aggregate(
[
{"$unwind": "$grades"},
{"$match": {}},
{"$group": {"_id": "$name", "Avg grade": {"$avg": "$grades.score"}}},
{"$sort": {"Avg grade": -1}}
]
)
for item in my_collection.find().sort("name").limit(10):
pprint(item)
for item in my_collection.find().sort("name", -1).limit(10):
pprint(item)

LEAVE A REPLY

Please enter your comment!
Please enter your name here