Python Data Persistence - PyMongo module

Neha Kumawat

10 months ago

Python Data Persistence | Insideaiml
Python Data Persistence | Insideaiml
MongoDB is a document-oriented NoSQL database. It is a cross-platform database distributed under a server-side public license. It uses JSON like documents as schema.
In order to provide the capability to store huge data, more than one physical servers (called shards) are interconnected, so that horizontal scalability is achieved. MongoDB database consists of documents.
A document is analogous to a row in a table of a relational database. However, it doesn't have a particular schema. The document is a collection of key-value pairs - similar to dictionary. However, the number of k-v pairs in each document may vary. Just as a table in a relational database has a primary key, the document in MongoDB database has a special key called "_id".
Before we see how MongoDB database is used with Python, let us briefly understand how to install and start MongoDB. Community and commercial version of MongoDB is available. The community version can be downloaded from www.mongodb.com/download-center/community.
Assuming that MongoDB is installed in c:\mongodb, the server can be invoked using the following command.
c:\mongodb\bin>mongod
The MongoDB server is active at port number 22017 by default. Databases are stored in data/bin folder by default, although the location can be changed by –dbpath option.
MongoDB has its own set of commands to be used in a MongoDB shell. To invoke a shell, use Mongo command.
x:\mongodb\bin>mongo
A shell prompt similar to MySQL or SQLite shell prompt, appears before which native NoSQL commands can be executed. However, we are interested in connecting MongoDB database to Python.
PyMongo module has been developed by MongoDB Inc itself to provide a Python programming interface. Use well-known pip utility to install PyMongo.
pip3 install pymongo
Assuming that MongoDB server is up and running (with mongod command) and is listening at port 22017, we first need to declare a MongoClient object. It controls all transactions between Python session and the database.
from pymongo import MongoClient
client=MongoClient()
Use this client object to establish a connection with MongoDB server.
client = MongoClient('localhost', 27017)
A new database is created with the following command.
db=client.newdb
MongoDB database can have many collections, similar to tables in a relational database. A Collection object is created by Create_collection() function.
db.create_collection('students')
Now, we can add one or more documents in the collection as follows −
from pymongo import MongoClient
client=MongoClient()
db=client.newdb
db.create_collection("students")
student=db['students']
studentlist=[{'studentID':1,'Name':'Juhi','age':20, 'marks'=100},
{'studentID':2,'Name':'dilip','age':20, 'marks'=110},
{'studentID':3,'Name':'jeevan','age':24, 'marks'=145}]
student.insert_many(studentlist)
client.close()
To retrieve the documents (similar to SELECT query), we should use find() method. It returns a cursor with the help of which all documents can be obtained.
students=db['students']
docs=students.find()
for doc in docs:
   print (doc['Name'], doc['age'], doc['marks'] )
To find a particular document instead of all of them in a collection, we need to apply a filter to find() method. The filter uses logical operators. MongoDB has its own set of logical operators as below −
MongoDB logical operators | Insideaiml
MongoDB logical operators | Insideaiml
For example, we are interested in obtaining a list of students older than 21 years. Using $gt operator in the filter for find() method as follows −
students=db['students']
docs=students.find({'age':{'$gt':21}})
for doc in docs:
   print (doc.get('Name'), doc.get('age'), doc.get('marks'))
PyMongo module provides update_one() and update_many() methods for modifying one document or more than one documents satisfying a specific filter expression.
Let us update the marks attribute of a document in which the name is Juhi.
from pymongo import MongoClient
client=MongoClient()
db=client.newdb
doc=db.students.find_one({'Name': 'Juhi'})
db['students'].update_one({'Name': 'Juhi'},{"$set":{'marks':150}})
client.close()
I hope you enjoyed reading this article and finally, you came to know about Python Data Persistence - PyMongo module.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review

BOT
Agent(Online)
We're Online!

Chat now for any query