As more and more data become available as unstructured or semi-structured, the need of managing them through NoSql database increases. Python can also interact with NoSQL databases in a similar way as is interacts with Relational databases. In this chapter we will use python to interact with MongoDB as a NoSQL database.
In order to connect to MongoDB, python uses a library known as pymongo. You can add this library to your python environment, using the below command from the Anaconda environment.
conda install pymongo
This library enables python to connect to MOngoDB using a db client. Once connected we select the db name to be used for various operations.
Inserting Data
To insert data into MongoDB we use the insert() method which is available in the database environment. First we connect to the db using python code shown below and then we provide the document details in form of a series of key-value pairs.
# Import the python libraries from pymongo import MongoClient from pprint import pprint # Choose the appropriate client client = MongoClient() # Connect to the test db db=client.test # Use the employee collection employee = db.employee employee_details = { 'Name': 'Raj Kumar', 'Address': 'Sears Streer, NZ', 'Age': '42' } # Use the insert method result = employee.insert_one(employee_details) # Query for the inserted document. Queryresult = employee.find_one({'Age': '42'}) pprint(Queryresult)
When we execute the above code, it produces the following result.
{u'Address': u'Sears Streer, NZ', u'Age': u'42', u'Name': u'Raj Kumar', u'_id': ObjectId('5adc5a9f84e7cd3940399f93')}
Updating Data
Updating an existing MongoDB data is similar to inserting. We use the update() method which is native to mongoDB. In the below code we are replacing the existing record with new key-value pairs. Please note how we are using the condition criteria to decide which record to update.
# Import the python libraries from pymongo import MongoClient from pprint import pprint # Choose the appropriate client client = MongoClient() # Connect to db db=client.test employee = db.employee # Use the condition to choose the record # and use the update method db.employee.update_one( {"Age":'42'}, { "$set": { "Name":"Srinidhi", "Age":'35', "Address":"New Omsk, WC" } } ) Queryresult = employee.find_one({'Age':'35'}) pprint(Queryresult)
When we execute the above code, it produces the following result.
{u'Address': u'New Omsk, WC', u'Age': u'35', u'Name': u'Srinidhi', u'_id': ObjectId('5adc5a9f84e7cd3940399f93')}
Deleting Data
Deleting a record is also straight forward where we use the delete method. Here also we mention the condition which is used to choose the record to be deleted.
# Import the python libraries from pymongo import MongoClient from pprint import pprint # Choose the appropriate client client = MongoClient() # Connect to db db=client.test employee = db.employee # Use the condition to choose the record # and use the delete method db.employee.delete_one({"Age":'35'}) Queryresult = employee.find_one({'Age':'35'}) pprint(Queryresult)
When we execute the above code, it produces the following result.
None
So we see the particular record does not exist in the db any more.
Next Topic:-Click Here
Pingback: Python - Relational Databases - Adglob Infosystem Pvt Ltd