- Python Data Science - Matplotlib
- Python Data Science - SciPy
- Python Data Science - Numpy
- Python Data Science - Pandas
- Python Data Science - Environment Setup
- Python Data Science - Getting Started
- Python Data Science - Home
Python Data Processing
- Python Stemming and Lemmatization
- Python word tokenization
- Python Processing Unstructured Data
- Python Reading HTML Pages
- Python Data Aggregation
- Python Data Wrangling
- Python Date and Time
- Python NoSQL Databases
- Python Relational databases
- Python Processing XLS Data
- Python Processing JSON Data
- Python Processing CSV Data
- Python Data cleansing
- Python Data Operations
Python Data Visualization
- Python Graph Data
- Python Geographical Data
- Python Time Series
- Python 3D Charts
- Python Bubble Charts
- Python Scatter Plots
- Python Heat Maps
- Python Box Plots
- Python Chart Styling
- Python Chart Properties
Statistical Data Analysis
- Python Linear Regression
- Python Chi-square Test
- Python Correlation
- Python P-Value
- Python Bernoulli Distribution
- Python Poisson Distribution
- Python Binomial Distribution
- Python Normal Distribution
- Python Measuring Variance
- Python Measuring Central Tendency
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python - NoSQL Databases
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 case you are new to MongoDB, you can learn it in our tutorial
In order to connect to MongoDB, python uses a pbrary known as pymongo. You can add this pbrary to your python environment, using the below command from the Anaconda environment.
conda install pymongo
This pbrary enables python to connect to MOngoDB using a db cpent. 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 pbraries from pymongo import MongoCpent from pprint import pprint # Choose the appropriate cpent cpent = MongoCpent() # Connect to the test db db=cpent.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 pbraries from pymongo import MongoCpent from pprint import pprint # Choose the appropriate cpent cpent = MongoCpent() # Connect to db db=cpent.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 pbraries from pymongo import MongoCpent from pprint import pprint # Choose the appropriate cpent cpent = MongoCpent() # Connect to db db=cpent.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.
Advertisements