Python MongoDB – A Complete Overeview

PyMongo

MongoDB is one of the most popular non-relational (also known as NoSQL database) databases. Non-relational or NoSQL databases do not have a fixed table structure or schema to be followed which makes the database very flexible and scalable. The data in NoSQL databases are stored in JSON-like format known as RSON. MongoDB is very convenient to use while dealing with large and unstructured data and hence it is the most widely used database in data analysis. It offers high speed and availability. In this article, let us see how can we connect our python script to MongoDB and perform desired operations.

Python MongoDB Driver

PyMongo is the native driver for connecting MongoDB and python. PyMongo has all the libraries to perform database operations from python code. Since pymongo is a low-level driver, it is fast and intuitive and provides more control. To install PyMongo, open your command line and type in the following command

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>python -m pip install pymongo

This command would install PyMongo. We could install PyMongo in our script and start accessing the MongoDB resources.

MongoDB databases

Now let us create a database in MongoDB. We will use the MongoClient() class of PyMongo to create the database. We will pass the correct localhost IP address and post to create the database. And use the client to give a desired name to the database.

from pymongo import MongoClient

#Creating a pymongo client
client = MongoClient('localhost', 27017)

#Getting the database instance
db = client['mongodb1']
print("Database created.")

#Verify the database
print("List of existing databases")
print(client.list_database_names())

Output

Database created.
List of existing databases:
['admin', 'config', 'local', 'mongodb1']

Creating a Collection

Inside a database, we can create multiple collections Collections can be compared to tables of the conventional database and we can store multiple records in the collection. Now, let us see how to create a collection inside a database. Also, note that our collection gets created when at least one document is inserted into it.

#create a collection named "students"
mycol = mydb["students"]

Insert into Collection

Records are called documents in MongoDB. To insert a document into the collection, we should use the insert_one() method. We can pass the created document as an argument in the insert_one method. Let us understand how to insert a document with an example.

#create a document
test = { "name": "Ripun", "class": "Seventh" }

#insert a document to the collection
x = mycol.insert_one(test)

Inserting multiple records

To insert multiple records into a collection, we can use insert_many() method. To implement this, we will first create a list with multiple documents and pass them on to insert_many() method.

mylist = [
  { “name”: “Amy”, “class”: “Seventh”},
  { “name”: “Hannah”, “class”: “Sixth”},
  { “name”: “Viola”, “class”: “Sixth”}] x= mycol.insert_many(mylist)

We can also insert them with their ids.

mylist = [ { "_id":1,"name": "Amy", "class": "Seventh"},
  { "_id":2,"name": "Hannah", "class": "Sixth"},
  { "_id":3,"name": "Viola", "class": "Sixth"}]   

x = mycol.insert_many(mylist)

print(x.inserted_ids)

Accessing the documents from collection

Now once the collection is structured and loaded with data, we would want to access them based on our requirements. To access the data, we can use the find() method.

find_one() method returns the first occurrence in the collection.

find() method returns all the occurrences in the collection. find() method when used without any parameter behaves the same way as Select all in SQL.

Output

x = mycol.find_one()

# This prints the first document
print(x)

for x in mycol.find():
  print(x)

Sometimes, we would want to retrieve only particular fields of the document. To include the field in the result the value of the parameter passed should be 1, if the value is 0 then it will be excluded from the result.

for x in mycol.find({},{ "_id": 0, "name": 1, "class": 1 }):
  print(x)

The above code will just return the name and the class field from our collection and excludes the id field.

Querying the MongoDB Database

We can use find() to retrieve results in more refined way by using the query object.

Operators

Following is the list of operators used in the queries in MongoDB.

OperationSyntaxExample
Equality{“key” : “value”}db.mycol.find({“by”:”tutorials point”})
Less Than{“key” :{$lt:”value”}}db.mycol.find({“likes”:{$lt:50}})
Less Than Equals{“key” :{$lte:”value”}}db.mycol.find({“likes”:{$lte:50}})
Greater Than{“key” :{$gt:”value”}}db.mycol.find({“likes”:{$gt:50}})
Greater Than Equals{“key” {$gte:”value”}}db.mycol.find({“likes”:{$gte:50}})
Not Equals{“key”:{$ne: “value”}}db.mycol.find({“likes”:{$ne:50}})

Example Code:

The following code retrieves the document where the name field is Sathish.

from pymongo import MongoClient

#Creating a pymongo client
client = MongoClient('localhost', 27017)

#Getting the database instance
db = client['sdsegf']

#Creating a collection
coll = db['example']

#Inserting document into a collection
data = [
   {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"},
   {"_id": "1002", "name": "Mukesh", "age": "27", "city": "Bangalore"},
   {"_id": "1003", "name": "Vel", "age": "28", "city": "Mumbai"},
   {"_id": "1004", "name": "Sathish", "age": "25", "city": "Pune"},
   {"_id": "1005", "name": "Rashiga", "age": "23", "city": "Delhi"},
   {"_id": "1006", "name": "Priya", "age": "26", "city": "Chennai"}
]
res = coll.insert_many(data)
print("Data inserted ......")

#Retrieving data
print("Documents in the collection: ")

for doc1 in coll.find({"name":"Sathish"}):
   print(doc1)

Output

Data inserted ......
Documents in the collection:
{'_id': '1004', 'name': 'Sathish', 'age': '25', 'city': 'Pune'}

Now let us retrieve the records with persons whose age is greater than 25. We will use the $gt operator to implement it.

for doc in coll.find({"age":{"$gt":"25"}}):
   print(doc)

Output

{“_id”: “1002”, “name”: “Mukesh”, “age”: “27”, “city”: “Bangalore”}
{“_id”: “1003”, “name”: “Vel”, “age”: “28”, “city”: “Mumbai”}

In the similar fashion, we can use $lt to filter the records with the value lesser than our specified value. We can also use these operators on a string. For example when we use “name”:{“$gt”:”J”} to retrieve all the records with names starting with ‘J’ or with the alphabets after that.

Delete Operation in Python MongoDB

We can use the delete_one() method to delete one document.

The first parameter of the delete_one() method is a query object which indicates the document to be deleted.

myquery = {"name" : "Mukesh"}

coll.delete_one(myquery)

To delete multiple documents, we can use the delete_many() method.

myquery = { "name": {"$regex": "^S"} }

x = coll.delete_many(myquery)

The above code will delete all the records where the person’s name starts with ‘S’ or the letters that are alphabetically placed after S.

To delete all documents in a collection, we can pass an empty query object to the delete_many() method. The below code will delete all the documents present in the collection.

x = coll.delete_many({})

If we would want to delete the whole collection itself, we can use the drop() method.

coll.drop()

Conclusion

In this article, we have seen about connecting MongoDB to python and performing various required and essential operations on it. The readers are strongly encouraged to get some hands-on experience with MongoDB and make themselves familiar on the syntax and various queries.

References

https://www.mongodb.com/languages/python

https://docs.mongodb.com/drivers/python/