Introduction to Non-SQL and MongoDB CRUD operations.

Introduction to No-SQL Databases

Hello, I am Tharuka Gayashan. Today we are going to talk about No-SQL databases. What is this No-SQL databases? No-SQL databases means non-tabular databases. There are several ways in which data exists in these No-SQL databases. Those are,

  • Document databases.
  • Key-Value databases
  • Wide-column stores
  • Graph databases

Any No-SQL database has its own set of features. Most No-SQL databases have the following features,

  • Ease of use for Developers.
  • Flexible Schema.
  • Fast queries due to the data model.
  • Horizontal Scaling.

Today we are going to talk about MongoDB, a popular document based database.

What is MongoDB ?

“MongoDB is a document database with scalability and flexibility that you want with the querying and indexing that you need.”

In MongoDB the data is stored as a Json type document. The main advantage here is that the structure of the data can be changed at any time. The documents here can be converted into objects so it is easy to work with them. Also, due to the efficiency of MongoDB, It is possible to access data faster. Also keep it mind that MongoDB runs on a spider monkey engine.

You can download MongoDB community edition using below link:

Here the MongoDB Shell is used to show examples of query operations. Also here I use student as a collection.

CRUD Operations

When entering data into a collection, data can be entered in two ways. That is, entering data individually or entering several at once. We will look at both of those methods with examples below.

The above is the query for inserting only one document. The red colour indicates the MongoDB query, the blue colour indicates the key of the Json object and the green value indicates the values.

You can separate the commas from each other in a square bracket [] as above and insert it as a Json object.

You can write the following query to view all the documents in the collection.

Both of the above methods are correct.

And there are times when we need to find unique data. So how do we find out?

  • How to find 13 year old children?. Then we can write a query like this.
  • How to find people named Anil and 12 year olds ? Then we can write a query like this.

There are two ways to update a document in MongoDB. Namely updateOne and updateMany. The process in all two methods is the same. We look at each one below with examples.

The above are the queries that apply to all two formats. As you can see in this method 2 we have to give three arguments.

Filter : For the argument referred to as filter, we need to provide the data we need to accurately identify the document we want to update. That is, to filter the required data.

Update : Give the update argument how the document should be changed.

Options: To option argument does not need to give anything.

  • Change Ajith’s address to Kandy.
  • Update the class for thirteen year olds to 8-C.

Examples 6 and 7 above show how to update documents. In these two sample queries we can see a command called $set. Below are some such commands and their function.

  • $push :- This operator is used to insert new data in case of updating an existing array in a document.
  • $set :- This $set operator replaces the value of a field with the specified value(Like update a value).

There are two ways to delete documents in MongoDB. These are,

deleteOne is used to delete only one document and deleteMany is used to delete several documents at once. This delete method should also introduce the documents/documents required to delete. Therefore the data required to delete with the help of a Json object should be introduced.

  • 071–4556546, Delete the person who owns this phone number

Above is the query to delete only one document.

  • Delete documents of 13 year olds.

According to the example above, deleteMany can delete the documents of all 13 year old students.

In today’s blog I talked about MongoDB, a very popular No-SQL database and its CRUD(Create,Read,Update,Delete) operations. In addition, there is a lot to learn in MongoDB. Currently, MongoDB is used to create many web applications. So I think these points will be important for someone who is new to MongoDB.

Thank you.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store