col.find_one({'name': 'Alessandro'}) #find first value equality

list(col.find({'name': 'Alessandro'})) #find all value equality

cursor = col.find({'name': 'Alessandro'}) #can also use it as a generator

cursor.next()

col.find_one({'name': 'Alessandro'}, {'phone': True}) #this is a projection

col.remove({'name': 'Alessandro'})
col.insert_one({'name': 'Alessandro', 'phone': '1'})
col.insert_one({'name': 'Alessandro', 'phone': '2'})
col.insert_one({'name': 'Alessandro', 'phone': '3'})

list(col.find({'name': 'Alessandro'}))

#remove all entries Alessandro and add 3 noew ones with different phone number



col.find_one({'_id': {'$gt': bson.ObjectId('575fd53c9d1fa214962baae1')}}) #retrieve first entries with number higher than ObjectID of the first entry

list(col.find({'name': {'$regex': '^O'}}))

col.update({'name': 'Alessandro'}, {'name': 'Giovanni'}) #changes one document from Alessandro to Giovanni

col.update({'name': 'Alessandro'}, {'name': 'Giovanni'}) #changes all document from Alessandro to Giovanni

"""
Explanation: As mongoDB is distributed, you need to specify on which node you want the document to be replicated. This is usually not a problem since you often read/write from a single node. The 'w' parameter controls this. w=0 just specifies that you want the data written as fast as possible on the node you are connected to without any confirmation to the user (for importing large dataset). This node then replicates the data around the cluster. The j parameter is to specify to backup the transaction or not to the journal (False makes it faster but less safe)
End of explanation
"""


col.find_one({'name': 'Giovanni'}

col.find({'name': 'Alessandro'}).count()

col.update({'name': 'Giovanni'}, {'$set': {'phone': '+23244322'}}) #

col.find_one({'name': 'Giovanni'})

doc = col.find_one({'name': 'Giovanni'})
doc['phone'] = 5
col.save(doc)
print doc

"""
Explanation: Updating the document replaces the whole original document
End of explanation
"""


col.update({'name': 'Alessandro'}, {'$set': {'name': 'Someone'}}, multi= True)

list(col.find({'name': 'Someone'}))

post = {
    'title': 'First post',
    'author':{
        'name': 'Alessandro',
        'surname': 'Molina',
        'avatar': 'https://blog.madmimi.com/wp-content/uploads/2014/06/gary_gravatar.png'
    },
    'tags': ['mongodb', 'web', 'scaling']
}

db.blog.insert(post)

db.blog.find_one({'tags': 'mongodb'})

db.blog.find_one({'author.name': 'Alessandro'}) #query on subdocs

class MyClass():
    def __init__(self, x, y):
        self.x = x
        self.y = y

c = MyClass(1,2)
print c.__dict__
db.blog.insert(c.__dict__)

"""
Explanation: Here's how to update all doc
End of explanation
"""


