In [1]:
import pymongo

In [2]:
pymongo.version


Out[2]:
'3.6.1'

In [3]:
from pymongo import MongoClient

In [9]:
client = MongoClient('localhost')

In [5]:
db = client.packt

In [6]:
testCollection = db.testCollection

In [7]:
testCollection.insert_one({'a': 1})


Out[7]:
<pymongo.results.InsertOneResult at 0x11137e3c8>

In [8]:
testCollection.find_one()


Out[8]:
{'_id': ObjectId('5b3512b4de17baeac545c57c'), 'a': 1}

In [10]:
testCollection.count()


Out[10]:
1

In [14]:
# Find Matching Collection
res = testCollection.insert_one({'b':2})

In [12]:
res.inserted_id


Out[12]:
ObjectId('5b3513b9de17baeac545c57e')

In [13]:
testCollection.find_one({'b':2})


Out[13]:
{'_id': ObjectId('5b3513b9de17baeac545c57e'), 'b': 2}

In [15]:
# Find record but do not return the _id as part of the result(projection operation)
testCollection.find_one({'b':2},{'_id':0})


Out[15]:
{'b': 2}

In [17]:
# Drop Collection and Database
db.drop_collection('testCollection')


Out[17]:
{'ns': 'packt.testCollection', 'nIndexesWas': 1, 'ok': 1.0}

In [18]:
db.drop_collection('testCollection')


Out[18]:
{'ok': 0.0,
 'errmsg': 'ns not found',
 'code': 26,
 'codeName': 'NamespaceNotFound'}

In [19]:
client.drop_database('packt')

In [20]:
# xrabne only retrieves values when needed --> GENERATORS

In [21]:
# Mongo Cursors
cur = db.collection.find()
for doc in cur:
    print(doc)

In [26]:
import random, string

In [28]:
letters = list(string.ascii_lowercase)

In [29]:
letters[:5]


Out[29]:
['a', 'b', 'c', 'd', 'e']

In [30]:
rs = testCollection.insert_many([{random.choice(letters):random.randint(1,10)} for i in range(10)])

In [31]:
rs.acknowledged


Out[31]:
True

In [32]:
rs.inserted_ids


Out[32]:
[ObjectId('5b3516adde17baeac545c580'),
 ObjectId('5b3516adde17baeac545c581'),
 ObjectId('5b3516adde17baeac545c582'),
 ObjectId('5b3516adde17baeac545c583'),
 ObjectId('5b3516adde17baeac545c584'),
 ObjectId('5b3516adde17baeac545c585'),
 ObjectId('5b3516adde17baeac545c586'),
 ObjectId('5b3516adde17baeac545c587'),
 ObjectId('5b3516adde17baeac545c588'),
 ObjectId('5b3516adde17baeac545c589')]

In [33]:
cur = testCollection.find()

In [34]:
type(cur)


Out[34]:
pymongo.cursor.Cursor

In [35]:
cur.count()


Out[35]:
10

In [36]:
for doc in cur:
    print(doc)


{'_id': ObjectId('5b3516adde17baeac545c580'), 'e': 1}
{'_id': ObjectId('5b3516adde17baeac545c581'), 'k': 6}
{'_id': ObjectId('5b3516adde17baeac545c582'), 'm': 3}
{'_id': ObjectId('5b3516adde17baeac545c583'), 'f': 5}
{'_id': ObjectId('5b3516adde17baeac545c584'), 'f': 5}
{'_id': ObjectId('5b3516adde17baeac545c585'), 'q': 3}
{'_id': ObjectId('5b3516adde17baeac545c586'), 's': 2}
{'_id': ObjectId('5b3516adde17baeac545c587'), 'c': 4}
{'_id': ObjectId('5b3516adde17baeac545c588'), 'i': 4}
{'_id': ObjectId('5b3516adde17baeac545c589'), 'x': 2}

In [37]:
cur.alive


Out[37]:
False

In [38]:
# cur is a connectio to database hence CLOSE it
cur.close()

In [39]:
db.drop_collection('testCollection')


Out[39]:
{'ns': 'packt.testCollection', 'nIndexesWas': 1, 'ok': 1.0}

In [40]:
# find(query, projection) -> query: return only conditions that match, projection: return only fields[values: 0,1]

Inserting a document with Nested Array


In [42]:
db = client.packt

In [43]:
coll = db.embedded

In [44]:
res = coll.insert_one({'ages': [30,42,12,19]})

In [52]:
coll.insert_one({'ages': [18,34,56,23,11]})


Out[52]:
<pymongo.results.InsertOneResult at 0x111485b88>

In [53]:
coll.find_one({'ages': 30}, {'_id': 0})


Out[53]:
{'ages': [30, 42, 12, 19]}

In [56]:
# Match arrayw ith conditinos on any element of array
coll.find_one({'ages': {'$gt': 42, '$lt': 20}}, {'_id': 0})


Out[56]:
{'ages': [18, 34, 56, 23, 11]}

In [58]:
# Match array with conditions on any single element of array
coll.find_one({'ages': {'$elemMatch': {'$lt': 22, '$gt': 15}}}, {'_id': 0})


Out[58]:
{'ages': [30, 42, 12, 19]}

In [62]:
# Match by array index
coll.find_one({'ages.1':42}, {'_id':0})


Out[62]:
{'ages': [30, 42, 12, 19]}

In [63]:
coll.find_one({'ages.1': {'$gt':40}}, {'_id': 0})


Out[63]:
{'ages': [30, 42, 12, 19]}

In [64]:
res = coll.insert_one({'personInfo': {'name': 'Alice', 'age':30}})
res = coll.insert_one({'personInfo': {'name': 'Chuks', 'age':53}})
res = coll.insert_one({'personInfo': {'name': 'Bob', 'age':42}})
res = coll.insert_one({'personInfo': {'name': 'Charlie', 'age':12}})
res = coll.insert_one({'personInfo': {'name': 'Davids', 'age':19}})

In [72]:
# Match by Age
coll.find_one({'personInfo.age': {'$gt': 40}}, {'_id':0})


Out[72]:
{'personInfo': {'name': 'Chuks', 'age': 53}}

In [73]:
# Match by Name
coll.find_one({'personInfo.name': 'Alice'}, {'_id': 0})


Out[73]:
{'personInfo': {'name': 'Alice', 'age': 30}}

In [74]:
# Updates: update_one() requires an operator e.g. $set, $inc, $unset, /$rename
# update_one({key: {'$inc': 10}})

In [75]:
# Comparison Operators: $gt, $lt, $neq, $lte

Aggregation


In [76]:
# Aggregate Commands: Computes stats e.g. sums, means
# Format: aggregate(pipeline = [<pipeline steps>])

In [78]:
# Example:
# aggregate([
#     {key: value},{"$group": 
#                  {"_id": null, 
#                   "total": {"$sum": value}}}
# ])

In [79]:
salaries = db.salaries

In [80]:
type(salaries)


Out[80]:
pymongo.collection.Collection

In [81]:
salaries.insert_one({'name': 'Alice', 'salary': 50000})
salaries.insert_one({'name': 'Bob', 'salary': 40000})
salaries.insert_one({'name': 'Charlie', 'salary': 60000})


Out[81]:
<pymongo.results.InsertOneResult at 0x11149b608>

In [82]:
# Update One record
salaries.update_one({'name': 'Alice'}, {'$set': {'salary': 55000}})


Out[82]:
<pymongo.results.UpdateResult at 0x1114ad208>

In [84]:
# Update a field value
salaries.find_one({'name': 'Alice'})


Out[84]:
{'_id': ObjectId('5b3542e3de17baeac545c591'), 'name': 'Alice', 'salary': 55000}

In [85]:
# Remove a field from Document
alice = salaries.update_one({'name': 'Alice'}, {'$unset': {'salary': ''}})

In [89]:
salaries.find_one({'name': 'Alice'}, {'_id': 0})


Out[89]:
{'name': 'Alice'}

Calculate total and mean salary


In [105]:
salaries.find().count()


Out[105]:
3

In [123]:
pipeline = []

In [124]:
pipeline.append({"$match": {"salary": {'$exists': "True"}}})

In [125]:
pipeline.append({'$group': {"_id":None, "avSalary": {"$avg": "$salary"}, "totalSalary": {"$sum":"$salary"}}})

In [127]:
cursor = salaries.aggregate(pipeline=pipeline)

In [128]:
cursor.next()


Out[128]:
{'_id': None, 'avSalary': 50000.0, 'totalSalary': 100000}

In [129]:
for d in salaries.find():
    print(d)


{'_id': ObjectId('5b3542e3de17baeac545c591'), 'name': 'Alice'}
{'_id': ObjectId('5b3542e3de17baeac545c592'), 'name': 'Bob', 'salary': 40000}
{'_id': ObjectId('5b3542e3de17baeac545c593'), 'name': 'Charlie', 'salary': 60000}

In [130]:
depts = db.sals

In [131]:
type(depts)


Out[131]:
pymongo.collection.Collection

In [132]:
depts.insert_one({'name':'Alice', 'salary': 50000, 'unit': 'legal'})
depts.insert_one({'name':'Bob', 'salary': 40000, 'unit': 'marketing'})
depts.insert_one({'name':'Charlie', 'salary': 60000, 'unit': 'communications'})
depts.insert_one({'name':'David', 'salary': 70000, 'unit': 'legal'})
depts.insert_one({'name':'Edwina', 'salary': 90000, 'unit': 'communications'})


Out[132]:
<pymongo.results.InsertOneResult at 0x1114d2148>

In [136]:
pipeline = []
# Aggregate based on unit
pipeline.append({'$group': {"_id": "$unit", "avSalary": {"$avg": "$salary"}, "totalSalary": {"$sum": "$salary"}}})

In [134]:
cur_depts = depts.aggregate(pipeline=pipeline)

In [135]:
for c in cur_depts:
    print(c)


{'_id': 'communications', 'avSalary': 75000.0, 'totalSalary': 150000}
{'_id': 'marketing', 'avSalary': 40000.0, 'totalSalary': 40000}
{'_id': 'legal', 'avSalary': 60000.0, 'totalSalary': 120000}

In [ ]: