Shivam
GDSC GHRCE
Published in
4 min readNov 17, 2020

--

Database, a term that everyone must have come across frequently. It is a term coined for the collection of information that is organized well enough to be easily accessed, managed, and updated using software called Database Management System(DBMS). There are various types of Databases SQL, NoSQL, Relational Database, Operational Database, Cloud Database, and many more.

MongoDB is a NoSQL database. The interesting contrast of the NoSQL database to the relational database is, it stores data in form of documents in collections unlike being composed of rows and tables.MongoDB stores documents in form of a JSON object. Those who are already well versed with the Relational Database concept can replace the terms Tables and Rows with Collections, Documents respectively.

To start working on MongoDb with Python, first, you need to create a cluster at MongoDB Atlas and create a free tier account there and make sure to add your IP address to the whitelist while creating a user.

Now let’s introduce you to the tool which would help you to work with MongoDB. PyMongo is a Python distribution for working with MongoDB in python. The current stable version available for the user is PyMongo 3.11.0.

Installing PyMongo

If your system is pre-equipped with pip, you can directly run the following command for installation

pip install pymongo

Connecting with Cluster

When you configure the MongoDB Atlas, you will see the connect button on the control panel. Then choose the Python driver to connect your application option.

Then you will find the connection string, you need to copy that and then paste it in the code below by just replacing the username and password.

import pymongo as pym
connection = pym.MongoClient("<the atlas connection string>")
*change username and pssword inside the connection string

Accessing or Creating a Database

import pymongo as pym
connection = pym.MongoClient("<the atlas connection string>")
test_db = connection.data

Here test_db is the variable that is assigned to the new database created or the pre-existing which is accessed named data.

Inserting Document

To insert documents into the collection we need to create a dictionary. The key part of the dictionary will be the fields of documents while the elements will be the value of documents.

Here we have already created a database named test_db, now we will create a collection named test_coll and fill it will a dictionary-created name test_dic.

import pymongo as pym
connection = pym.MongoClient("<the atlas connection string>")
test_db = connection.data
collection = test_db.test_coll
test_dic = {'name': 'Shivam', 'college': 'GHRCE','branch': 'AI'}
inserted = collection.insert_one(test_dic)

For inserting multiple documents, insert the data in the dictionary and use insert_many instead of insert_one.

Updating Documents

Many times we need to update the existing database due to some changes. For this, we can use the update function. Similar to insert we can use update in two form update_one and update_many.

import pymongo as pym
connection = pym.MongoClient("<the atlas connection string>")
test_db = connection.data
collection = test_db.test_coll
query = {'sec': 'A'}
new_update = {'$set': {'sec': 'C'}}
updated = collection.update_many(query, new_user)

Finding Documents

PyMongo provides a function to find documents under the entire database.

import pymongo as pym
connection = pym.MongoClient("<the atlas connection string>")
test_db = connection.data
collection = test_db.test_coll
found_data = collection.find_one({'name': 'Shivam'})
print(found_data)

Accessing information using unique id

All the documents are stored under an auto-generated unique id. Here we have a database of students, now if we want to access the branch of the student using its unique id we can run the following code.

import pymongo as pym
connection = pym.MongoClient("<the atlas connection string>")
test_db = connection.data
scollection = test_db.test_coll
document = collection.find_one({'_id':ObjectId(xxxxxxxxxx)}, {'name': 0, 'college': 0, 'sec':0})
print(document)

Deleting Documents

We can delete single or multiple documents using the delete functions. We just need to be very careful about passing an empty dictionary to delete_many() as it will delete entire documents from the collection. You have the option of using delete_one or delete_many according to the need.

import pymongo as pym
connection = pym.MongoClient("<the atlas connection string>")
test_db = connection.data
collection = test_db.test_coll
del = collection.delete_many({'name': 'Shivam'})

Conclusion

I hope you all would have enjoyed the blog. You all are now well equipped to start using MongoDB and deploy it in practical use. Learning DBMS is something that would help you in your entire career. If you wish to learn more about this, I would recommend you check out this link.

--

--

Shivam
GDSC GHRCE

BTech in Artificial Intelligence |Core team member at DSC GHRCE