NoSQL Database Handling

Arvind Hari
Geek Culture
Published in
3 min readJun 14, 2021

--

The Pythonic way of dealing with MongoDB

Obviously, from the mongodb.com

SQL databases like MySQL, Oracle etc. follow a fixed schema per table. Schema refers to the number of columns and the type of data that is fed into the columns. Schema represents the structure of table with the SQL database. The issue with this approach is that, if we want to suddenly increase the number of columns, we need to create a whole new table altogether with the new structure. It is not very efficient to handle adhoc bulk amounts of data using SQL.

This is where, NoSQL (Not just SQL) databases come into picture. Here the schema is not a fixed entity. We can easily scale the data. Python provides facilities to deal with NoSQL databases directly. In this article, we discuss establishment of connection between Python and the NoSQL database MongoDB.

ineruon.ai has come up with a very helpful article to set up the the cloud version of MongDB known as ATLAS. In this article, we are using the free version of MongoDB ATLAS.

All the code snippets and their outputs are avaialble @ Github.

Establishing connection between Python and MongoDB

MongoDB ATLAS Connector for Python (pymongo)

Creating a Database and Collection in MongoDB

Just an interim observation

Unlike MySQL, an empty collection does not get created. See the image below. Collection is visible only when a record is entered.

Empty Cluster (even after adding a databse)

Inserting a Record into a Collection

The inputs are fed in terms of key-value pairs. We call it a JSON (Java Script Object Notation), but for the time being, let us just consider this a dictionary.

Unique ID

Auto created Unique ObjectIDs

All the records are now visible iside the actual database in MongoDB ATLAS. Just showing one for simplicity.

_id is created automatically. This is unique per record. However, this can be manually over ridden. But within a collection, the user must ensure, all induvidual records have unique IDs

Overriding Unique _ids

Overriding Object IDs
User defined _id in MongoDB\

Extracting data from MongoDB

Data extraction from MongoDB using filters in Queries

Updating Data in MongoDB

This can be achieved by passing two dictionaries to the update_many(old_dict,new_dict)

This function finds the first document (record) that matches with the query and update it with an object defining the new values of the document, i.e Updates a single record within the collection based on the filter.

Updated data is now appended to the Collection’s Record

Note the new data updated t the colelction that has _id : 1

Deleting Records (Documents) from a Collection

Dropping a complete Collection

We also have a ready made option to drop a complete collection from a database.

Database with 2 collections Cars and Cars_Again before dropping

Database with just 1 collection Cars_Again after dropping

So be it, welcome to the world of NoSQL databases

Signing off.

The code is available @ Github. Do catch me on Linkedin for more.

--

--