Introduction to MongoDB with CRUD Operations

Introduction to MongoDB with CRUD Operations

Introduction to MongoDB with Basic CRUD operations.

Table Content:

  • What is MongoDB?
  • Mapping of SQL terminology to NoSQL terminology.
  • What is mean by CRUD operation?
  • CRUD Operations.

What is MongoDB?

A MongoDB is an open-source NoSQL database. NoSQL database means we can store different documents having a different schema inside the same collection. in SQL terms, we can store different records having different fields in the same table.

Mapping of SQL terminology to NoSQL terminology.

Below is the mapping of terminology from SQL to NoSQL. Like, tables in SQL are the same as collections in NoSQL.

SQLNOSQL
DatabaseDatabase
TablesCollection
RowDocument
ColumnField
JOINSEmbedded Documents

Here is a sample document for reference

{
  _id: ObjectId(1ek95cd0542l),
  first_name : Alex,
  last_name : Paul,
  email : [
    {
      type: parent,
      email: alexmom@gmail.com
    },
    {
      type: home,
      email: alex@gmail.com
    }
    ],
  mobile_number : +91 8080808090
}

What is mean by CRUD operation?

CRUD operations refer to the basic insert, read, update and delete operations on the collection or table.

CRUD Operations
C => Create
R => Read
U => Update
D => DELETE

So, I hope now you have a basic understanding of the NoSQL database, collection, and documents. For the CRUD operation, we need a database. Creating a database is very easy in MongoDB. Go into the mono shell and use the following command.

 > use college

This will create a database college if it's already not present. Now we can move forward with our CRUD operations.

Create

The create operation is used to insert or add new documents to the collection. In the same way as a database, if the collection does not exist, then it will create a new collection into the database. For adding or inserting documents MongoDB provides two methods. One for inserting a single document and another for inserting many documents.

db.collection.insertOne()
db.collection.insertMany()

Now, we will use our college database. We will insert the document into the student collection.

Example One: We will insert a single document into the student collection, using insertOne() method.

> use college

> db.student.insertOne({
    first_name : Alex,
    last_name : Bale,
    branch : electrical,
    fees : paid
    email : [
        {
            type: parent,
            email: alexmom@gmail.com
        },
        {
            type: home,
            email: alex@gmail.com
        }
    ],
    mobile_number : +91 8080808090
    })

Example Two: We will insert multiple documents into the student collection, using insertMany() method.

db.student.insertMany(
{
    first_name : Jake,
    last_name : Paul,
    branch : Mechanical,
    fees : paid
    email : [
        {
            type: home,
            email: jake@gmail.com
        }
    ],
    mobile_number : +91 8080808080
},
{
    first_name : Jane,
    last_name : Graham,
    branch : CSE,
    fees : paid
    email : [
        {
            type: home,
            email: jane@gmail.com
        }
    ],
    mobile_number : +91 8080808070
})

Read

The read operation is used to fetch the documents from the given collection. Same as like create operation, you can either fetch a single document or all documents from the collection.

db.collection.findOne()
db.collection.find()

Let's take the example of each, first, we will fetch a single record.

> db.student.findOne({ first_name: "Jane" })

//output
{
    _id : ObjectId(1ek95cd0545l)
    first_name : Jane,
    last_name : Graham,
    branch : CSE,
    fees : paid
    email : [
        {
            type: home,
            email: jane@gmail.com
        }
    ],
    mobile_number : +91 8080808070
}

Now, let's take the example of fetching all the records from the collection. I have used the pretty() method just to format the documents.

> db.student.find().pretty()

//output
{
    _id : ObjectId(1ek95cd0542l)
    first_name : Alex,
    last_name : Bale,
    branch : electrical,
    fees : paid
    email : [
        {
            type: parent,
            email: alexmom@gmail.com
        },
        {
            type: home,
            email: alex@gmail.com
        }
    ],
    mobile_number : +91 8080808090
}
{
    _id : ObjectId(1ek95cd0544l)
    first_name : Jake,
    last_name : Paul,
    branch : Mechanical,
    fees : paid
    email : [
        {
            type: home,
            email: jake@gmail.com
        }
    ],
    mobile_number : +91 8080808080
}
{
    _id : ObjectId(1ek95cd0545l)
    first_name : Jane,
    last_name : Graham,
    branch : CSE,
    fees : paid
    email : [
        {
            type: home,
            email: jane@gmail.com
        }
    ],
    mobile_number : +91 8080808070
}

Update

Just like the name, the update operation is used to update the existing documents in the collection. We can perform update operations using two methods. One is used to update the single document and the other is used to update more than one documents.

db.collection.updateOne()
db.collection.updateMany()

Let's take the example of both of these methods. First, we will see, updateOne() method.

> db.student.updateOne({name: "Jake"}, {$set : { last_name: "Silva" }})

> db.student.findOne({name : "Jake"})

//output
{
    _id : ObjectId(1ek95cd0544l)
    first_name : Jake,
    last_name : Silva,
    branch : Mechanical,
    fees : paid
    email : [
        {
            type: home,
            email: jake@gmail.com
        }
    ],
    mobile_number : +91 8080808080
}

Let's see updateMany() method example,

> db.student.updateMany({}, {$set : { fees : "remaining" }})

//output
{
    _id : ObjectId(1ek95cd0542l)
    first_name : Alex,
    last_name : Bale,
    branch : electrical,
    fees : remaining
    email : [
        {
            type: parent,
            email: alexmom@gmail.com
        },
        {
            type: home,
            email: alex@gmail.com
        }
    ],
    mobile_number : +91 8080808090
}
{
    _id : ObjectId(1ek95cd0544l)
    first_name : Jake,
    last_name : Paul,
    branch : Mechanical,
    fees : remaining
    email : [
        {
            type: home,
            email: jake@gmail.com
        }
    ],
    mobile_number : +91 8080808080
}
{
    _id : ObjectId(1ek95cd0545l)
    first_name : Jane,
    last_name : Graham,
    branch : CSE,
    fees : remaining
    email : [
        {
            type: home,
            email: jane@gmail.com
        }
    ],
    mobile_number : +91 8080808070
}

Delete

The delete operation is used to remove the documents from a collection. You can use delete operations using two methods.


db.collection.deleteOne()
db.collection.deleteMany()

Let's take the example of the first method, deleteOne().

> db.student.deleteOne({name : "Jake"})

>db.student.find().pretty()

//output
{
    _id : ObjectId(1ek95cd0542l)
    first_name : Alex,
    last_name : Bale,
    branch : electrical,
    fees : remaining
    email : [
        {
            type: parent,
            email: alexmom@gmail.com
        },
        {
            type: home,
            email: alex@gmail.com
        }
    ],
    mobile_number : +91 8080808090
}
{
    _id : ObjectId(1ek95cd0545l)
    first_name : Jane,
    last_name : Graham,
    branch : CSE,
    fees : remaining
    email : [
        {
            type: home,
            email: jane@gmail.com
        }
    ],
    mobile_number : +91 8080808070
}

Let's take the example of deleteMany() method.

> db.student.deleteOne({})

These are the simple CRUD operations in MongoDB. There are a lot of other methods available in MongoDB. For further reading, you can read MongoDB documentation. Thanks for reading.