What is MongoDB?

MongoDB is a NoSQL database which is used to store data in document form.

It is an OpenSource Document oriented Database. It uses JSON (JavaScript Object Notation) for displaying documents.  And BSON (Binary Object Notation) for storage and retrieval of data.

Features: 

  1. Flexible schema
  2. Heavy read
  3. Allows documents inside a document
  4. Array functionalities
  5. Support for CRUD operations with aggregation

Why MongoDB?

MongoDB is Flexible Document based NoSQL Database. It has capability to work with document.

With fast changing digital world and vast amount of data generating daily MongoDB provides tools to process new age Data. We can easily Create, Retrieve, Update, Delete (CRUD) using MongoDB.

Managing Data also becomes simple using MongoDB.

Python provides simple and efficient tools to connect to MongoDB. Using pymongo module we can perform all basic operations on MongoDB document.

So, without wasting much time let us dive into world of Python and MongoDB.

Connecting to MongoDB

from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27018")
mydatabase = myclient['Database_Name']

Inserting into Database

Insert one row



from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27018")
mydatabase = myclient['demodb']
mytable = mydatabase['demotable']
doc = {'id':'1','name':'abc'}
res = mytable.insert_one(doc)
print(res.inserted_id)   #print inserted id 

Insert multiple row



from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27018")
mydatabase = myclient['demodb']
mytable = mydatabase['demotable']
doc = [{'id':'2','name':'def'},
       {'id':'3','name':'xyz'}]
res = mytable.insert_many(doc)
print(res.inserted_id)   #print inserted id 

Retrieval of data



from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27018")
mydatabase = myclient['demodb']
mytable = mydatabase['demotable']

#fetching the first document
doc = mytable.find_one()
print(doc)

#fetching all the documents
doc_1 = mytable.find()
for doc in doc_1:
    print(doc)

#where conditon
wh = mytable.find({'name':'xyz'})
for doc in wh:
    print(doc)

#using regex
reg = mytable.find({'name':{'$regex':'^a'}})
for doc in reg:
    print(doc)

#displaying certain column
col = mytable.find({},{'_id':0,'name':1})
for doc in col:
    print(doc)

Sorting data

from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27018")
mydatabase = myclient['demodb']
mytable = mydatabase['demotable']
doc = mytable.find({}).sort('id',1)
for d in doc:
    print(d)

Limit number of Rows



from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27018")
mydatabase = myclient['demodb']
mytable = mydatabase['demotable']
doc = mytable.find({}).limit(2)
for d in doc:
    print(d)

Deleting data



from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27018")
mydatabase = myclient['demodb']
mytable = mydatabase['demotable']

#deleting one document
done = mytable.delete_one({'id':'1'})

deleting all document
dmany = mytable.delete_many({'id':'2'})

dpc = mytable.find({})
for d in doc:
    print(d)


Updating data



from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27018")
mydatabase = myclient['demodb']
mytable = mydatabase['demotable']

#updating one document
upone = mytable.update_one({'id':'1'},{'$set':{'name':'pqr'}})

#updating many documents
upmany = mytable.update_many({'id':{'$gt':'1'}},{'$set':{'quotient':5}})

doc = mytable.find({})
for d in doc:
    print(d)


There are many options for updating and retrieving data. Try other options too.

Hope it helps You

Happy Learning!

Leave a Reply