MongoDB with Python - pyMongo - Basic IV

Update Operation

In [1]:
from pymongo import MongoClient, ASCENDING, DESCENDING
import pprint
import random
In [2]:
con = MongoClient("mongodb://mdbuser:[email protected]:27017,cluster0-shard-00-01-mswpe.mongodb.net:27017,\
cluster0-shard-00-02-mswpe.mongodb.net:27017/Cluster0?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true")
con.list_database_names()
Out[2]:
['Cluster0', 'admin', 'local']
In [15]:
db = con.Cluster0
db.list_collection_names()
Out[15]:
['user', 'movies_scratch', 'testidx', 'movies_initial']

Single Update - this operation will update the very first document as find

In [17]:
# Check how many distict value in Job Key
cur=db.user.find({})
cur.distinct('Job')
Out[17]:
['Student',
 'DA',
 'Doctor',
 'Painter',
 'Retired',
 'Teacher',
 'HouseWife',
 'Engineer',
 'Singer',
 'Generator',
 'Architect',
 'Physicist',
 'Nanny',
 'Bio-tech',
 'Manager',
 'Cashier']
In [4]:
db.user.update_one({'Job':'Student'}, {'$set':{'Address':'Young St.'}})
Out[4]:
<pymongo.results.UpdateResult at 0x1b3999bcd08>
In [5]:
# let's see what have been updated
db.user.find_one({'Job':'Student'})
Out[5]:
{'_id': ObjectId('5c16e863817810ed3fc5e5f9'),
 'Fname': 'atul',
 'Lname': 'Singh',
 'Grade': 12.0,
 'College': 'SGM',
 'Job': 'Student',
 'Address': 'Young St.'}

Bulk update:update_many records

In [6]:
pprint.pprint(db.user.update_many({'Job':'Teacher'}, {'$set':{'Address':'Carlton St.'}}))
<pymongo.results.UpdateResult object at 0x000001B3999EEE88>
In [7]:
pprint.pprint(db.user.find_one({'Job':'Teacher'}))
{'Address': 'Carlton St.',
 'Age': 58,
 'City': 'Toronto',
 'Job': 'Teacher',
 'Name': 'Rajesh',
 '_id': ObjectId('5c26b381802bd99ce0af5117')}

Bulk update with updated count

In [11]:
cur=db.user.update_many({'Job':'Architect'}, {'$set':{'Address':'Balliol Ave.'}})
In [12]:
# Raw Status of update statement
cur.raw_result
Out[12]:
{'n': 10,
 'nModified': 10,
 'opTime': {'ts': Timestamp(1546485619, 10), 't': 1},
 'electionId': ObjectId('7fffffff0000000000000001'),
 'ok': 1.0,
 'operationTime': Timestamp(1546485619, 10),
 '$clusterTime': {'clusterTime': Timestamp(1546485619, 10),
  'signature': {'hash': b'\xa4+\x8a9\xf1oB\xdd\xbf\x01/\xff\xce\xce\xe5\xea\xf8\x9e*\xd1',
   'keyId': 6633075164693135361}},
 'updatedExisting': True}
In [13]:
# Count of documents which matched with the update where condition
cur.matched_count
Out[13]:
10
In [14]:
# Count of documents which have been updated
cur.modified_count
Out[14]:
10

Bulk Update with upsert (if document is not exist for update, it will insert the document)

In [18]:
cur=db.user.update_many({'Job':'Physicist'}, {'$set':{'Address':'Eglinton Ave.', 'Job':'Architect'}}, upsert= True)
print(cur.matched_count)
print(cur.modified_count)
print(cur.upserted_id)
print(cur.raw_result)
7
7
None
{'n': 7, 'nModified': 7, 'opTime': {'ts': Timestamp(1546485778, 7), 't': 1}, 'electionId': ObjectId('7fffffff0000000000000001'), 'ok': 1.0, 'operationTime': Timestamp(1546485778, 7), '$clusterTime': {'clusterTime': Timestamp(1546485778, 7), 'signature': {'hash': b'\xff0\xc5\x18z\x86\xfe\x99\xcc\x1b\x0cS\xe2\x1e\x99TA\xd8\xf9\x97', 'keyId': 6633075164693135361}}, 'updatedExisting': True}
In [19]:
db.user.find_one({'Job':'Physicist'})
In [21]:
db.user.find_one({'Job':'Architect', 'Address':'Eglinton Ave.'})
Out[21]:
{'_id': ObjectId('5c26b386802bd99ce0af511f'),
 'name': 'User3',
 'Age': 107,
 'Gender': 'F',
 'City': 'Toronto',
 'Job': 'Architect',
 'Address': 'Eglinton Ave.'}

Bulk Update with \$set and \$unset
$set - will add the field if not existing else update

$unset - will remove the filed

In [22]:
cur=db.user.update_many({'Job':'Nanny'}, {'$set':{'Address':'Yong Ave.', 'Job':'Physicist'}})
print(cur.matched_count)
print(cur.modified_count)
print(cur.raw_result)
pprint.pprint(db.user.find_one({'Job':'Physicist'}))
4
4
{'n': 4, 'nModified': 4, 'opTime': {'ts': Timestamp(1546485873, 4), 't': 1}, 'electionId': ObjectId('7fffffff0000000000000001'), 'ok': 1.0, 'operationTime': Timestamp(1546485873, 4), '$clusterTime': {'clusterTime': Timestamp(1546485873, 4), 'signature': {'hash': b'#(\x8f\xca\xab\x96\x0e\x90W\x98\xad\xe5\xfd\x1d\x11yI\xb8R\x92', 'keyId': 6633075164693135361}}, 'updatedExisting': True}
{'Address': 'Yong Ave.',
 'Age': 109,
 'City': 'Toronto',
 'Gender': 'M',
 'Job': 'Physicist',
 '_id': ObjectId('5c26b386802bd99ce0af5122'),
 'name': 'User6'}
In [23]:
cur=db.user.update_many({'Job':'Architect'}, {'$set':{'Address':'Yong Ave.', 'Job':'BellBoy'}, '$unset':{'Age':1}})
print(cur.matched_count)
print(cur.modified_count)
print(cur.upserted_id)
print(cur.raw_result)
pprint.pprint(db.user.find_one({'Job':'BellBoy'}))
17
17
None
{'n': 17, 'nModified': 17, 'opTime': {'ts': Timestamp(1546485897, 17), 't': 1}, 'electionId': ObjectId('7fffffff0000000000000001'), 'ok': 1.0, 'operationTime': Timestamp(1546485897, 17), '$clusterTime': {'clusterTime': Timestamp(1546485897, 17), 'signature': {'hash': b"\x98z\xb8*\x8f\x82\x8b\xe5'\xaa\xd2+z#\xc3\xc8\x07u\xf2\x8c", 'keyId': 6633075164693135361}}, 'updatedExisting': True}
{'Address': 'Yong Ave.',
 'City': 'Toronto',
 'Gender': 'F',
 'Job': 'BellBoy',
 '_id': ObjectId('5c26b386802bd99ce0af511d'),
 'name': 'User1'}

Update - Replace all the fields - replace_one

In [24]:
db.user.find_one({'Name':'Anu'})
Out[24]:
{'_id': ObjectId('5c26fee1802bd99ce0af5150'),
 'Name': 'Anu',
 'Age': 20,
 'Job': 'Teacher',
 'City': 'Delhi',
 'Address': 'Carlton St.'}
In [25]:
db.user.replace_one({'Name':'Anu'},
              {'Name':'Akshita',
              'Age':26,
              'Job':'Student',
              'City':'Kolkal'})
Out[25]:
<pymongo.results.UpdateResult at 0x1b399df7408>
In [26]:
pprint.pprint(db.user.find_one({'Name':'Akshita'}))
pprint.pprint(db.user.find_one({'Name':'Anu'}))
{'Age': 26,
 'City': 'Kolkal',
 'Job': 'Student',
 'Name': 'Akshita',
 '_id': ObjectId('5c26fee1802bd99ce0af5150')}
None

\$inc - increment the value of column

In [27]:
db.user.update_one({'Name':'Akshita'}, {'$inc':{'Age':-3}})
Out[27]:
<pymongo.results.UpdateResult at 0x1b399a09dc8>
In [28]:
pprint.pprint(db.user.find_one({'Name':'Akshita'}))
{'Age': 23,
 'City': 'Kolkal',
 'Job': 'Student',
 'Name': 'Akshita',
 '_id': ObjectId('5c26fee1802bd99ce0af5150')}

Update the array field

In [29]:
db.user.update_one({'Name':'Ashley'},
              {'$set':{'Name':'Ashley','Age':26, 'City':'Kanpur', 'grades':[10, 15, 17, 9],
                       'details':[{'grade':12, 'mean':8},{'grade':11, 'mean':17}]}, '$unset':{'Provice':1}},
                  upsert=True)

pprint.pprint(list(db.user.find({'Name':'Ashley'})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 17}],
  'grades': [10, 15, 17, 9]}]
In [30]:
db.user.update_one({'Name':'Ashley'},
                  {'$set':{'grades.$[element]':12}},
                  array_filters=[{"element": { '$gt': 12 }}])
Out[30]:
<pymongo.results.UpdateResult at 0x1b3999bcac8>
In [31]:
pprint.pprint(list(db.user.find({'Name':'Ashley'})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 17}],
  'grades': [10, 12, 12, 9]}]

update dictionary like array field

In [32]:
db.user.update_one({'Name':'Ashley'},
                  {'$set':{'details.$[element].mean':12}},
                  array_filters=[{"element.mean": { '$gt': 12 }}])
pprint.pprint(list(db.user.find({'Name':'Ashley'})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}],
  'grades': [10, 12, 12, 9]}]

Update Specific indexed value in array field
If the key and value are existing, it will updated else inserted

In [40]:
# add one element at index 4 grade - 14
db.user.update_one({'Name':'Ashley'},
                  {'$set':{'grades.4':14}})
pprint.pprint(list(db.user.find({'Name':'Ashley'})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}],
  'grades': [10, 12, 12, 9, 14]}]
In [41]:
# add one element at index 4 Subject - Math
# as we can see, index 3 has been updated as None (NULL)
db.user.update_one({'Name':'Ashley'},
                  {'$set':{'Subject.4':'Math'}})
pprint.pprint(list(db.user.find({'Name':'Ashley'})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art', None, 'Math'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}],
  'grades': [10, 12, 12, 9, 14]}]

Push Modifier \$push
"$push" adds an element to the end of an array if the specified key exists and creates a new array if it does not.
It basically used to add multiple value into array

In [42]:
# inserted an array [11,15] as a value at index 5
db.user.update_one({'Name':'Ashley'},
                  {'$push':{'grades':[11, 15]}})
pprint.pprint(list(db.user.find({'Name':'Ashley'})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art', None, 'Math'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}],
  'grades': [10, 12, 12, 9, 14, [11, 15]]}]
In [43]:
# pushed no 16, 17 into grade key 
db.user.update_one({'Name':'Ashley'},
                  {'$push':{'grades':{'$each':[16, 17]}}})
pprint.pprint(list(db.user.find({'Name':'Ashley'})))
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art', None, 'Math'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}],
  'grades': [10, 12, 12, 9, 14, [11, 15], 16, 17]}]

addToSet

The "$addToSet" operator adds a value to an array unless the value is already present, if the value is in the array it will not append.

In [49]:
db.user.update_one({'Name':'Ashley'},
                {'$addToSet':{'Subject':{'$each':['Art', 'Bio-Tech']}}})
pprint.pprint(list(db.user.find({'Name':'Ashley'})))                   
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art', None, 'Math', 'Bio-Tech'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}],
  'grades': [10, 12, 12, 9, 14, [11, 15], 16, 17]}]

pop - Pop will pull the element from array from last (by default : 1), from start if specified (-1)

In [50]:
# from last
db.user.update_one({'Name':'Ashley'},
                   {'$pop':{'grades':1}})
pprint.pprint(list(db.user.find({'Name':'Ashley'})))              
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art', None, 'Math', 'Bio-Tech'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}],
  'grades': [10, 12, 12, 9, 14, [11, 15], 16]}]
In [52]:
# from start
db.user.update_one({'Name':'Ashley'},
                   {'$pop':{'grades':-1}})
pprint.pprint(list(db.user.find({'Name':'Ashley'})))              
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art', None, 'Math', 'Bio-Tech'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'details': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}],
  'grades': [12, 12, 9, 14, [11, 15], 16]}]

Rename a Field name - rename

In [54]:
# from start
db.user.update_one({'Name':'Ashley'},
                   {'$rename':{'details':'grades_in_detail'}})
pprint.pprint(list(db.user.find({'Name':'Ashley'})))              
[{'Age': 26,
  'City': 'Kanpur',
  'Marital_Status': None,
  'Name': 'Ashley',
  'State': 'UP',
  'Subject': ['French', 'English', 'Art', None, 'Math', 'Bio-Tech'],
  '_id': ObjectId('5c26fb7932245fbaaa24e47e'),
  'grades': [12, 12, 9, 14, [11, 15], 16],
  'grades_in_detail': [{'grade': 12, 'mean': 8}, {'grade': 11, 'mean': 12}]}]

Delete Operation

  • delete_many - will delete the multiple documents matching the condition
  • delete_one - will delete the very first found document matching the condition
In [55]:
cur=db.user.delete_many({'Name':'Ashley'})
In [56]:
print(cur.deleted_count)
1
In [57]:
print(cur.raw_result)
{'n': 1, 'opTime': {'ts': Timestamp(1546488036, 1), 't': 1}, 'electionId': ObjectId('7fffffff0000000000000001'), 'ok': 1.0, 'operationTime': Timestamp(1546488036, 1), '$clusterTime': {'clusterTime': Timestamp(1546488036, 1), 'signature': {'hash': b'O\xd6u\xfb<\xbe\xc2\xc7M\x06\xdbT\xdc\xd2\xff\xdc\xfd\xfd\x8en', 'keyId': 6633075164693135361}}}