Introduction to MongoDB and PyMongo in Python

What is NoSQL?

NoSQL is a database that is generally used to store unstructured  data. Earlier data was mainly stored in a tabular form comprising rows  and columns. SQL was used to handle the structured data in the past. You  can look at the list of our free SQL courses to learn the language effectively.

With the advancement in technology, developers moved to a more  flexible database. NoSQL stored the data in the form of documents. This  helps in managing a large amount of unstructured data easily and  effectively.

We used Relational Database Management Systems to work on SQL  which was quite good for small data packages. As the companies are  collecting much-unstructured data it became almost impossible to handle  it with SQL as a whole.

Some important Features of NoSQL Database

  • No need for defining the structure at the beginning
  • The form of data is nonrelational which means the data flow is not directional.
  • Every other record can have its own different substructure
  • Mainly stored in Key-Value format.
  • Can be scaled horizontally without degrading the performance.

Now you are known to the NoSQL database we will be looking at MongoDB.

In this tutorial, you’ll learn:

  • What MongoDB is
  • How to install and run MongoDB
  • How to connect Python with MongoDB database using Pymongo
  • What is Pymongo
  • Perform CRUD operations in MongoDB

What is MongoDB?

MongoDB is a NoSQL, Open Source, Cross-Platform, Document Oriented  Database system. It stores data in key-value pairs rather than  traditional row columns and tabular approach.

Companies are moving to MongoDB architecture because it  provides a scalable database with ultimately high performance. Most of  the latest programming languages use object functions and the earlier  database approaches were not able to store objects directly much  efficiently.

MongoDB deals in Document-Collection storage which can store objects directly. It makes the process of developing robust applications very much easy.

Here are some amazing features of MongoDB:

  • The content management is Semi-Structured
  • Most of the standard query types are supported
  • The system is highly flexible and portable
  • Easily scalable by adding more machines and documents.
  • It is not mandatory to keep similar fields in documents
  • Document structure could be easily updated
  • Better Memory management than traditional RDBMS
  • Works under automatic failure support
  • MongoDB support multiple storage engines to store data efficiently.

You can download MongoDB from here: MongoDB Download

What is PyMongo?

PyMongo is the official driver for running MongoDB code in the Python environment. You can go through this article for learning MongoDB.  The code we write uses PyMongo as a connection between the environment  and the database. We will be looking at some code to learn some basic  operations with PyMongo in Python.

Any Python IDE could be used but here I’m using Jupyter Notebook for running up the codes.

1. Installing PyMongo

By running the following code we can easily install PyMongo through pip.

 

2.Importing PyMongo

3. Python MongoDB Connection with MongoClient

We need to create a connection with MongoClient to create a MongoDB  instance. This is the first step to use PyMongo in Python environment.

4. Checking All Available Databases

Run the following code to get all available databases in your connection.

5. Creating a Database

New Database can easily be created with the help of MongoClient.

6. Deleting a Database

Any prebuilt PyMongo database could be deleted with the following code:

7. Create Collection in a Database

A database can contain multiple collections having different attributes. You can create one using the code below

8. Inserting Document in Collection

There are two main options to insert documents in the collections.  Either you can insert single entry at a time or you can insert multiple  entries in the PyMongo collection.

9. View Data From Collection

You can view data in multiple manners and this is what makes MongoDB  and PyMongo an ultimate Python function. This way all our collections  and documents can be easily viewed in multiple ways.

Viewing Single Row: You can view a single row using find_one() function. It will only return the first row of document.

Viewing All Rows: View all rows using find_many() function.

Viewing only a few Rows: View only selected fields by providing 0 to the ones we want to hide and 1 to those which we want to see.

10. Filtering Data in PyMongo

PyMongo makes it very much easy to filter data and there are many  ways to perform it. One way is to filter data based on the field and its  value.

Here we have used “$gt” for finding all the values greater than the specific attribute. There are other modifiers as well:

GT- Greater ThannLT – Less ThannET – Equal To

Data can also be filtered with the help of Regular Expressions and here is the code for performing that:

Here we have filtered the data and selected on those elements which start with the letter ‘G’.

11. Querying with ObjectID

We can easily fetch data with the ObjectID which is a unique identifier for each element.

12. Counting Documents

In MongoDB, the rows are known as Documents and Columns are generally  stated as collections. So we are counting the number of Documents(Rows)  in our collection.

13. Sorting Documents

Data can be easily sorted with the help of sort() function. The  default case for sorting is in ascending order but we can always change  that by providing a parameter ‘-1’.

14. Updating Documents

Any field could be updated with PyMongo clients in just a few lines  of codes. Either you can update a single row at a time or update  multiple rows at ones.

15. PyMongo Limit

The function limit( ) is used to limit the number of rows we are currenty accessing or viewing.

16. Deleting Documents

We can delete the number of inserted rows in our collection anytime.  Function “object.deleted_count” can be used to get the number of deleted  rows.

17. Dropping Whole Collection

The whole PyMongo collection could be deleted by drop command as stated below:

mycol.drop()

About Author :

connect with me : https://www.linkedin.com/in/vignesh-sekar-sujatha-02aa9b125/

March 19, 2021