In [12]:
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')

In [13]:
client.phonebook.collection_names()


Out[13]:
[u'people']

In [14]:
db = client.phonebook

In [15]:
col = db.people

In [22]:
col.insert({'name': 'Alessandro', 'phone': '+39333333'})


/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.
  if __name__ == '__main__':
Out[22]:
ObjectId('575fc3369d1fa214962baad3')

In [24]:
import bson
bson.ObjectId('575fc3369d1fa214962baad3').generation_time


Out[24]:
datetime.datetime(2016, 6, 14, 8, 41, 26, tzinfo=<bson.tz_util.FixedOffset object at 0x103650750>)

In [28]:
bson.ObjectId() #this creates a new bson object everytime it is executed


Out[28]:
ObjectId('575fcec29d1fa214962baad8')

In [30]:
col.insert({'name': 'someoneelese', 'phone': '+34434345', 'phone2': '+232456'}, w=0, j=False) #this document has a different format from the first one (one more field)


/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.
  if __name__ == '__main__':
Out[30]:
ObjectId('575fd0ee9d1fa214962baada')

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)


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


Out[32]:
{u'_id': ObjectId('575fc1cf9d1fa214962baad0'),
 u'name': u'Alessandro',
 u'phone': u'+39333333'}

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


Out[34]:
[{u'_id': ObjectId('575fc1cf9d1fa214962baad0'),
  u'name': u'Alessandro',
  u'phone': u'+39333333'},
 {u'_id': ObjectId('575fc1e09d1fa214962baad1'),
  u'name': u'Alessandro',
  u'phone': u'+39333333'},
 {u'_id': ObjectId('575fc32b9d1fa214962baad2'),
  u'name': u'Alessandro',
  u'phone': u'+39333333'},
 {u'_id': ObjectId('575fc3369d1fa214962baad3'),
  u'name': u'Alessandro',
  u'phone': u'+39333333'}]

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

In [44]:
cursor.next()


Out[44]:
{u'_id': ObjectId('575fc1e09d1fa214962baad1'),
 u'name': u'Alessandro',
 u'phone': u'+39333333'}

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


Out[46]:
{u'_id': ObjectId('575fc1cf9d1fa214962baad0'), u'phone': u'+39333333'}

In [50]:
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


/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: remove is deprecated. Use delete_one or delete_many instead.
  if __name__ == '__main__':
Out[50]:
[{u'_id': ObjectId('575fd53c9d1fa214962baae1'),
  u'name': u'Alessandro',
  u'phone': u'1'},
 {u'_id': ObjectId('575fd53c9d1fa214962baae2'),
  u'name': u'Alessandro',
  u'phone': u'2'},
 {u'_id': ObjectId('575fd53c9d1fa214962baae3'),
  u'name': u'Alessandro',
  u'phone': u'3'}]

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


Out[53]:
{u'_id': ObjectId('575fd53c9d1fa214962baae2'),
 u'name': u'Alessandro',
 u'phone': u'2'}

In [54]:
list(col.find({'name': {'$regex': '^O'}}))


Out[54]:
[]

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


/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: update is deprecated. Use replace_one, update_one or update_many instead.
  if __name__ == '__main__':
Out[55]:
{u'n': 1, u'nModified': 1, u'ok': 1, 'updatedExisting': True}

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


/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: update is deprecated. Use replace_one, update_one or update_many instead.
  if __name__ == '__main__':
Out[57]:
{u'n': 1, u'nModified': 1, u'ok': 1, 'updatedExisting': True}

Updating the document replaces the whole original document


In [62]:
col.find_one({'name': 'Giovanni'}


Out[62]:
{u'_id': ObjectId('575fd53c9d1fa214962baae1'),
 u'name': u'Giovanni',
 u'phone': u'+23244322'}

In [59]:
col.find({'name': 'Alessandro'}).count()


Out[59]:
1

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


/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: update is deprecated. Use replace_one, update_one or update_many instead.
  if __name__ == '__main__':
Out[60]:
{u'n': 1, u'nModified': 1, u'ok': 1, 'updatedExisting': True}

In [64]:
col.find_one({'name': 'Giovanni'})


Out[64]:
{u'_id': ObjectId('575fd53c9d1fa214962baae1'),
 u'name': u'Giovanni',
 u'phone': u'+23244322'}

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


{u'phone': 5, u'_id': ObjectId('575fd53c9d1fa214962baae1'), u'name': u'Giovanni'}
/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:3: DeprecationWarning: save is deprecated. Use insert_one or replace_one instead
  app.launch_new_instance()

Here's how to update all doc


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


/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: update is deprecated. Use replace_one, update_one or update_many instead.
  if __name__ == '__main__':
Out[68]:
{u'n': 1, u'nModified': 1, u'ok': 1, 'updatedExisting': True}

In [69]:
list(col.find({'name': 'Someone'}))


Out[69]:
[{u'_id': ObjectId('575fd53c9d1fa214962baae3'),
  u'name': u'Someone',
  u'phone': u'3'}]

In [71]:
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)


/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:11: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.
Out[71]:
ObjectId('575fd9c79d1fa214962baae4')

In [72]:
db.blog.find_one({'tags': 'mongodb'})


Out[72]:
{u'_id': ObjectId('575fd9c79d1fa214962baae4'),
 u'author': {u'avatar': u'https://blog.madmimi.com/wp-content/uploads/2014/06/gary_gravatar.png',
  u'name': u'Alessandro',
  u'surname': u'Molina'},
 u'tags': [u'mongodb', u'web', u'scaling'],
 u'title': u'First post'}

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


Out[73]:
{u'_id': ObjectId('575fd9c79d1fa214962baae4'),
 u'author': {u'avatar': u'https://blog.madmimi.com/wp-content/uploads/2014/06/gary_gravatar.png',
  u'name': u'Alessandro',
  u'surname': u'Molina'},
 u'tags': [u'mongodb', u'web', u'scaling'],
 u'title': u'First post'}

In [89]:
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__)


{'y': 2, 'x': 1}
/Users/adrianopagano/Desktop/Big_Dive/BigDive5/DataScience/PythonDataScience/lib/python2.7/site-packages/ipykernel/__main__.py:8: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.
Out[89]:
ObjectId('575feaa99d1fa214962baae6')

In [ ]: