What MongoDB and how can one use it?

Satya131113
3 min readSep 12, 2021

MonogoDB is a No-SQL Database. OK.So what is this No-SQL Database means?

In the world we live digital data is stored mostly in forms when it comes to Database storage either in structured format of tables with all the fields predefined and exist for sure, then comes the other format , Unstructured format (kind of) which can have many values which might come new or might have a value for pre existing header . The first type of storing is SQL Database and the second one is the No-SQL.

So MongoDB is a NoSQL Database which stores the data in form of documents.

If we want to store some user name then the way to store it is as follows

Mongo Database → Collection → User Document → User Name

A Document is basically like a row in table, like say if for a student marks need to stored then we can store it as each subject as a document with marks of that subject over a period of time or tests as headers

Ok, if a teacher is strict and conducted more exams then that subject document ill have more headers , if one teacher forgot to conduct some tests then that subject document might have less headers

MongoDB can be used to store all this data for a student in a collection with student’s name. And whole class data can be created in a Mongo Database.

How create a MongoDB ?

Go to MongoDB website , signup for a account,create a project, create a DB from the provided plans. Create all the collections you wanted.

How do I use some programming do write or read data from the collections?

So with python we can use ‘pymongo’ library to do these. Import pymongo, create a connection with DB using pymongo and connection uri which one can get from MongoDB Atlas. Just click connect and select the language which you need it in. It also give the required drivers if selected for. Then you can easily connect to any collection in the DB from your application.

Let variable ‘db’ connects you to the Mongo Database.

MongoDB documents are similar to JSON files or Dictionaries in Python. So

CRUD — Create,Read,Update,Delete

db[collection].insert_one(data) → Here data is either a variable with python dictionary structure

db[collection].insert_many(datas) → Here the datas will be list of all the json structured data

Inserting is direct because we don’t need to specify anything about path or something like condition but for other operatios we need to define the path or conditions needed to perform them, so we will use “query ” to specify this path.

A query can be anything from checking for a value to checking the values in headers with some conditional statements.

db[collection].findOne(query)

db[collection].findMany(query)

db[collection].updateOne(query, {$set : data}, upsert=True/False)

db[collection].updateMany(query, {$set : data}, upsert=True/False)

$set part will set the data for the specified paths, upsert is to check if one need to add new document incase the query didn’t us any

We can also use filter along with query as a parameter to specify which fields we want in the response

db[collection].deleteOne(query, filter)

db[collection].delteMany(query, filter)

We can also perform these operations in bulk using bulk_write. All we need to do is make a list of all the operations that we need to perform and them send that as paramenter in bulk_write operation

bulk_write_list = [……] → It can contain all the operations one want to perform on a collection

db[collection].bulk_write(bulk_write_list)

Beside these we have aggregation queries which are very good way to get data with a wide range of conditions and variations.

One can use aggregate commands to select a wide range of paths, filters, limiting data, sorting data and many more. One can also get data from multiple collections using “lookup” property in aggregation commands.

We can perform aggregation command taking one collection as base collection.

db[collection].aggregate(aggregate_command)

Will write a seperate post to describe aggregate commands.

--

--