Welcome
INTRODUCTION TO MONGODB IN PYTHON
Donny Winston
Instructor
JavaScript Object Notation (JSON)
Objects {} Arrays []
String keys & values Series of values [value1, value2,...]
{'key1':value1, 'key2':value2,...}
Order of values is important
Order of values is not important
[
{ "instructor_1",
'id': 12345, "instructor_2",
'name': 'Donny Winston', ...
'instructor': true ]
},
INTRODUCTION TO MONGODB IN PYTHON
JavaScript Object Notation (JSON)
{ Values
'people': [
{ 'id': 12345, Strings 'name':'Donny Winston'
'name': 'Donny Winston',
'instructor': true,
Numbers 'id': 12345
'tags': ['Python', 'MongoDB']
true / false
},
null
{ 'id': 54321
'name': 'Guido van Rossum' Another array
'instructor':false 'tags': ['Python', 'MongoDB']
'tags': null
},
Another object
]
} [{ 'id': 12345, ...},...]
INTRODUCTION TO MONGODB IN PYTHON
JSON <> Python
JSON Python
Objects Dictionaries dict
Arrays Lists list
Values:
· strings str
· _numbers _ int , float
· true / false True / False
· null None
· other objects/arrays other dict / list
--
INTRODUCTION TO MONGODB IN PYTHON
JSON <> Python <> MongoDB
MongoDB JSON Python
Databases Objects Dictionaries
↳Collections Arrays Lists
↳↳Documents Objects Dictionaries
↳↳↳Subdocuments Objects Dictionaries
Value types
Value
↳↳↳ Values + datetime ,
types
regex...
INTRODUCTION TO MONGODB IN PYTHON
The Nobel Prize API data(base)
import requests
from pymongo import MongoClient
# Client connects to "localhost" by default
client = MongoClient()
# Create local "nobel" database on the fly
db = client["nobel"]
for collection_name in ["prizes", "laureates"]:
# collect the data from the API
response = requests.get(
"http://api.nobelprize.org/v1/{}.json".\
format(collection_name[:-1] ))
# convert the data to json
documents = response.json()[collection_name]
# Create collections on the fly
db[collection_name].insert_many(documents)
INTRODUCTION TO MONGODB IN PYTHON
Accessing databases and collections
Using []
# client is a dictionary of databases
db = client["nobel"]
# database is a dictionary of collections
prizes_collection = db["prizes"]
Using .
# databases are attributes of a client
db = client.nobel
# collections are attributes of databases
prizes_collection = db["prizes"]
INTRODUCTION TO MONGODB IN PYTHON
Count documents in a collection
# Use empty document {} as a filter # Find one document to inspect
filter = {} doc = db.prizes.find_one(filter)
# Count documents in a collection
{'_id': ObjectId('5bc56145f35b634065ba1996'),
n_prizes = db.prizes.count_documents(filter)
'category': 'physics',
n_laureates = db.laureates.count_documents(filter)
'laureates': [{'firstname': 'Arthur',
'id': '960',
590 'motivation': '"for the optical tweezers and their
934 application to biological systems"',
'share': '2',
'surname': 'Ashkin'},
{'firstname': 'Gérard',
'id': '961',
'motivation': '"for their method of generating
high-intensity, ultra-short optical pulses"',
...
INTRODUCTION TO MONGODB IN PYTHON
Let's practice!
INTRODUCTION TO MONGODB IN PYTHON
Finding documents
INTRODUCTION TO MONGODB IN PYTHON
Donny Winston
Instructor
An example "laureates" document
{'_id': ObjectId('5b9ac94ff35b63cf5231ccb1'),
'born': '1845-03-27',
'bornCity': 'Lennep (now Remscheid)',
'bornCountry': 'Prussia (now Germany)',
'bornCountryCode': 'DE',
'died': '1923-02-10',
'diedCity': 'Munich',
'diedCountry': 'Germany',
'diedCountryCode': 'DE',
'firstname': 'Wilhelm Conrad',
'gender': 'male',
'id': '1',
'prizes': [{'affiliations': [{'city': 'Munich',
'country': 'Germany',
'name': 'Munich University'}],
'category': 'physics',
'motivation': '"in recognition of the extraordinary services '
'he has rendered by the discovery of the '
'remarkable rays subsequently named after him"',
'share': '1',
'year': '1901'}],
'surname': 'Röntgen'}
INTRODUCTION TO MONGODB IN PYTHON
Filters as (sub)documents
Count documents by providing a lter
document to match.
filter_doc = {
'born': '1845-03-27',
'diedCountry': 'Germany',
'gender': 'male',
'surname': 'Röntgen'
}
db.laureates.count_documents(filter_doc)
INTRODUCTION TO MONGODB IN PYTHON
INTRODUCTION TO MONGODB IN PYTHON
Simple filters
db.laureates.count_documents({'gender': 'female'})
48
db.laureates.count_documents({'diedCountry': 'France'})
50
db.laureates.count_documents({'bornCity': 'Warsaw'})
INTRODUCTION TO MONGODB IN PYTHON
Composing filters
filter_doc = {'gender': 'female',
'diedCountry': 'France',
'bornCity': 'Warsaw'}
db.laureates.count_documents(filter_doc)
db.laureates.find_one(filter_doc)
{'_id': ObjectId('5bc56154f35b634065ba1be9'),
'born': '1867-11-07',
'bornCity': 'Warsaw',
'bornCountry': 'Russian Empire (now Poland)',
'bornCountryCode': 'PL',
'died': '1934-07-04',
'diedCity': 'Sallanches',
'diedCountry': 'France',
'diedCountryCode': 'FR',
'firstname': 'Marie',
...
INTRODUCTION TO MONGODB IN PYTHON
Query operators
INTRODUCTION TO MONGODB IN PYTHON
Query operators
Value in a range $in: <list> Query syntax:
db.laureates.count_documents({
{
'diedCountry': {
# Match a single value exactly:
'$in': ['France', 'USA']}})
'field_name1': value1,
258 # Use operators:
'field_name2': {
Not equal $ne : <value> $operator1: value1,
$operator2: value2,
db.laureates.count_documents({ ... # more operators
'diedCountry': { },
'$ne': 'France'}}) ... # more fields
}
872
INTRODUCTION TO MONGODB IN PYTHON
Query operators
Comparison: Query syntax:
> : $gt , ≥ : $gte
{
< : $lt , ≤ : $lte # Match a single value exactly:
'field_name1': value1,
db.laureates.count_documents({
'diedCountry': {
# Use operators:
'$gt': 'Belgium',
'field_name2': {
'$lte': 'USA'}})
$operator1: value1,
$operator2: value2,
453 ... # more operators
},
... # more fields
453
}
(Strings are compared lexicographically)
INTRODUCTION TO MONGODB IN PYTHON
Let's Practice!
INTRODUCTION TO MONGODB IN PYTHON
Dot notation: reach
into substructure
INTRODUCTION TO MONGODB IN PYTHON
Donny Winston
Instructor
A functional density
db.laureates.find_one({ db.laureates.count_documents({
"firstname": "Walter", "prizes.affiliations.name": (
"surname": "Kohn"}) "University of California")})
{'born': '1923-03-09', 34
'bornCity': 'Vienna',
'bornCountry': 'Austria',
'firstname': 'Walter', db.laureates.count_documents({
'prizes': [ "prizes.affiliations.city": (
{'affiliations': [ "Berkeley, CA")})
{'city': 'Santa Barbara, CA',
'country': 'USA',
'name': ('University of ' 19
'California')
}],
'category': 'chemistry',
'motivation': (
'"for his development of the '
'density-functional theory"'),
'share': '2',
'year': '1998'
}],
'surname': 'Kohn',
...} # showing partial document
INTRODUCTION TO MONGODB IN PYTHON
No Country for Naipaul
db.laureates.find_one({'surname': 'Naipaul'}) db.laureates.count_documents({"bornCountry": {"$exists": False}})
{'_id': ObjectId('5b9ec791f35b63093c3d98b7'), 31
'born': '1932-08-17',
'died': '2018-08-11',
'diedCity': 'London',
'diedCountry': 'United Kingdom',
'diedCountryCode': 'GB',
'firstname': 'Sir Vidiadhar Surajprasad',
'gender': 'male',
'id': '747',
'prizes': [{'affiliations': [[]],
'category': 'literature',
'motivation': ('"for having united perceptive narrative and '
'incorruptible scrutiny in works that compel us '
'to see the presence of suppressed histories"'),
'share': '1',
'year': '2001'}],
'surname': 'Naipaul'}
INTRODUCTION TO MONGODB IN PYTHON
Multiple prizes
db.laureates.count_documents({})
922
db.laureates.count_documents({"prizes": {"$exists": True}})
922
db.laureates.count_documents({"prizes.0": {"$exists": True}})
922
db.laureates.count_documents({"prizes.1": {"$exists": True}})
INTRODUCTION TO MONGODB IN PYTHON
Multiple-prize winners
INTRODUCTION TO MONGODB IN PYTHON
On to exercises!
INTRODUCTION TO MONGODB IN PYTHON