In [1]:
import pymongo
In [2]:
pymongo.version
Out[2]:
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]:
In [8]:
testCollection.find_one()
Out[8]:
In [10]:
testCollection.count()
Out[10]:
In [14]:
# Find Matching Collection
res = testCollection.insert_one({'b':2})
In [12]:
res.inserted_id
Out[12]:
In [13]:
testCollection.find_one({'b':2})
Out[13]:
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]:
In [17]:
# Drop Collection and Database
db.drop_collection('testCollection')
Out[17]:
In [18]:
db.drop_collection('testCollection')
Out[18]:
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]:
In [30]:
rs = testCollection.insert_many([{random.choice(letters):random.randint(1,10)} for i in range(10)])
In [31]:
rs.acknowledged
Out[31]:
In [32]:
rs.inserted_ids
Out[32]:
In [33]:
cur = testCollection.find()
In [34]:
type(cur)
Out[34]:
In [35]:
cur.count()
Out[35]:
In [36]:
for doc in cur:
print(doc)
In [37]:
cur.alive
Out[37]:
In [38]:
# cur is a connectio to database hence CLOSE it
cur.close()
In [39]:
db.drop_collection('testCollection')
Out[39]:
In [40]:
# find(query, projection) -> query: return only conditions that match, projection: return only fields[values: 0,1]
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]:
In [53]:
coll.find_one({'ages': 30}, {'_id': 0})
Out[53]:
In [56]:
# Match arrayw ith conditinos on any element of array
coll.find_one({'ages': {'$gt': 42, '$lt': 20}}, {'_id': 0})
Out[56]:
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]:
In [62]:
# Match by array index
coll.find_one({'ages.1':42}, {'_id':0})
Out[62]:
In [63]:
coll.find_one({'ages.1': {'$gt':40}}, {'_id': 0})
Out[63]:
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]:
In [73]:
# Match by Name
coll.find_one({'personInfo.name': 'Alice'}, {'_id': 0})
Out[73]:
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
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]:
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]:
In [82]:
# Update One record
salaries.update_one({'name': 'Alice'}, {'$set': {'salary': 55000}})
Out[82]:
In [84]:
# Update a field value
salaries.find_one({'name': 'Alice'})
Out[84]:
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]:
In [105]:
salaries.find().count()
Out[105]:
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]:
In [129]:
for d in salaries.find():
print(d)
In [130]:
depts = db.sals
In [131]:
type(depts)
Out[131]:
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]:
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)
In [ ]: