How is this different to normal Mongo query?

Normal query parameters may look like: {Age : {$gt:50}}

With PyMongo,field names and operators are encapsulated in "" {"Age": {"$lt": 100}}

In [8]:
import pymongo
import dns 

############

# DB=Kodey
# Collection=mongo

############

client = pymongo.MongoClient("mongodb+srv://username:password@cluster0-n5qsi.mongodb.net/test?retryWrites=true&w=majority")

#Show databases
client.list_database_names()

#Insert some records
db = client.kodey
db.mongo.insert_one({"title": "My first post", "body": "This is the body of my first blog post", "slug": "first-post"})

#Print the contents of the collection
for document in client.kodey.mongo.find():
    print(document)
{'_id': ObjectId('5df4f83c964841ec13eea100'), 'name': 'Charlie Brown', 'Age': 100.0, 'Likes': ['ice cream', 'sausages'], 'size': {'h': 178.0, 'w': 85.0, 'measure': 'metric'}}
{'_id': ObjectId('5df4f919964841ec13eea101'), 'name': 'Michael Smith', 'Age': 25.0, 'size': {'h': 130.0, 'w': 35.0, 'measure': 'metric'}}
{'_id': ObjectId('5df4f919964841ec13eea102'), 'name': 'Kimi Raikkonen', 'Age': 40.0, 'Likes': ['racing']}
{'_id': ObjectId('5df4f919964841ec13eea103'), 'name': 'Ronald Mcdonald', 'Age': 150.0, 'Likes': ['ice cream', 'burgers', 'fries'], 'size': {'h': 180.0, 'w': 105.0, 'measure': 'metric'}}
{'_id': ObjectId('5df69c8181787f581214a371'), 'title': 'My first post', 'body': 'This is the body of my first blog post', 'slug': 'first-post'}
{'_id': ObjectId('5df69e6581787f581214a373'), 'title': 'My first post', 'body': 'This is the body of my first blog post', 'slug': 'first-post'}
{'_id': ObjectId('5df69e7381787f581214a375'), 'title': 'My first post', 'body': 'This is the body of my first blog post', 'slug': 'first-post'}
In [10]:
#Search for specific record
for document in client.kodey.mongo.find({"name": "Charlie Brown"}):
    print(document)
{'_id': ObjectId('5df4f83c964841ec13eea100'), 'name': 'Charlie Brown', 'Age': 100.0, 'Likes': ['ice cream', 'sausages'], 'size': {'h': 178.0, 'w': 85.0, 'measure': 'metric'}}
In [13]:
#Use a regular expression
import re
regx = re.compile(".*Kimi.*", re.IGNORECASE)

#Search for specific record
for document in client.kodey.mongo.find({"name": regx}):
    print(document)
{'_id': ObjectId('5df4f919964841ec13eea102'), 'name': 'Kimi Raikkonen', 'Age': 40.0, 'Likes': ['racing']}
In [14]:
#Using the less than operator
for document in client.kodey.mongo.find({"Age": {"$lt": 100}}):
    print(document)
{'_id': ObjectId('5df4f919964841ec13eea101'), 'name': 'Michael Smith', 'Age': 25.0, 'size': {'h': 130.0, 'w': 35.0, 'measure': 'metric'}}
{'_id': ObjectId('5df4f919964841ec13eea102'), 'name': 'Kimi Raikkonen', 'Age': 40.0, 'Likes': ['racing']}
In [16]:
#using unwind to explode the array
for document in client.kodey.mongo.aggregate( [ { "$unwind" : "$Likes" } ] ):
    print(document)
{'_id': ObjectId('5df4f83c964841ec13eea100'), 'name': 'Charlie Brown', 'Age': 100.0, 'Likes': 'ice cream', 'size': {'h': 178.0, 'w': 85.0, 'measure': 'metric'}}
{'_id': ObjectId('5df4f83c964841ec13eea100'), 'name': 'Charlie Brown', 'Age': 100.0, 'Likes': 'sausages', 'size': {'h': 178.0, 'w': 85.0, 'measure': 'metric'}}
{'_id': ObjectId('5df4f919964841ec13eea102'), 'name': 'Kimi Raikkonen', 'Age': 40.0, 'Likes': 'racing'}
{'_id': ObjectId('5df4f919964841ec13eea103'), 'name': 'Ronald Mcdonald', 'Age': 150.0, 'Likes': 'ice cream', 'size': {'h': 180.0, 'w': 105.0, 'measure': 'metric'}}
{'_id': ObjectId('5df4f919964841ec13eea103'), 'name': 'Ronald Mcdonald', 'Age': 150.0, 'Likes': 'burgers', 'size': {'h': 180.0, 'w': 105.0, 'measure': 'metric'}}
{'_id': ObjectId('5df4f919964841ec13eea103'), 'name': 'Ronald Mcdonald', 'Age': 150.0, 'Likes': 'fries', 'size': {'h': 180.0, 'w': 105.0, 'measure': 'metric'}}

You can use MongoDB query language to reduce data size & do manipulations in Python

In [21]:
from pandas.io.json import json_normalize
response = client.kodey.mongo.aggregate( [ { "$unwind" : "$Likes" } ] )
data = json_normalize(response)
In [22]:
data
Out[22]:
_id name Age Likes size.h size.w size.measure
0 5df4f83c964841ec13eea100 Charlie Brown 100.0 sausages 178.0 85.0 metric
1 5df4f919964841ec13eea102 Kimi Raikkonen 40.0 racing NaN NaN NaN
2 5df4f919964841ec13eea103 Ronald Mcdonald 150.0 ice cream 180.0 105.0 metric
3 5df4f919964841ec13eea103 Ronald Mcdonald 150.0 burgers 180.0 105.0 metric
4 5df4f919964841ec13eea103 Ronald Mcdonald 150.0 fries 180.0 105.0 metric
In [ ]: