At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

MongoDB – An Ultimate Database Guide For Beginners

  • By Yuvaraj
  • April 17, 2020
  • 818 Views

MongoDB is a NoSQL (Not Only Structured Query Language) database. It is used to store unstructured data and it consists of collections and documents. The flexibility of NoSQL is that documents can be created without having a definite structure. This blog gives you a great outlook on this database. And I am sure that it gives a great introduction to MongoDB for beginners.

Collections

MongoDB is used to store a list of documents in a collection. It’s similar to tables in SQL.

Documents

The document is used to store data into the MongoDB database. It’s similar to rows in SQL.

List of data types supported in MongoDB

Data Type Description
string Maybe an empty string or a combination of characters
integer Digits
boolean Logical values (True or False)
double A type of floating point number
array A list of values

MongoDB VS RDBMS

Select, insert, update, and delete operations can be performed on a collection. The following table will help you to understand the concept more easily.

MongoDB RDBMS
Collection Table
Document / Object Records / Rows
Key Column
Value Value

One of the best advantages of MongoDB is that it accepts JSON data.
Prerequisites: The latest version of MongoDB should be installed (Version 4.2.3)

ALSO READ: MongoDB In Golang With Examples – A Beginner’s Guide

MongoDB Queries

Creating & Selecting the database

Syntax:

use databaseName

Query:
If the database exists it will switch to a particular database otherwise it will create a new database and it will switch.

> use customer
switched to db customer

Check the current database with the below syntax

db
> db
customer

Show the list of the database available

It will not show the database “customer” which we have created now because we have to insert one document (record) into the collection.

> show dbs
admin (empty)
local 0.078GB

After inserting the document in the database, it will show the database which we have created.

> show dbs
admin (empty)
customer 0.078GB
local

Drop Database

If you want to delete the database which you created, “customer”.
Query:

> db.dropDatabase()
{ "dropped" : "customer", "ok" : 1 }

To create the collection (Tables)

db.createCollection(name,options)

Name: Name of the collection to be created
Options: Specify options about memory size and indexing
Query:

> db.createCollection("customer_details")
{ "ok" : 1 }

To view a list of collections available

Query:

> show collections
customer_details
System.indexes

Insert document

To insert the document into the collection
Syntax:

db.DATABASE NAME.insert ({“JSON”})

Query:

db.customer_details.insert ({"name": "foo"})
WriteResult({ "nInserted" : 1 })

customer_details is the collection name, if the collection does not exist in the database, MongoDB will create the new collection and insert the documents into the newly created collection.
Query:

>db.customer_details.insert({"name":"john","place":"ind","status":"active"})
WriteResult({ "nInserted" : 1 }

To insert multiple documents we can use an array of documents(JSON) to insert into the collection.
Example

>db.customer_details.insert([{"name":"jay","place":"ind","status":"active"},
{"name":"peter","place":"canada","status":"active"},
{"name":"yuvi","place":"usa","status":"inactive"}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

Query document

To find the document (select the records)
To query the documents from the collection, we use the find method.
Syntax:

db.COLLECTION_NAME.find()

Query:
The find method will display the documents in an unstructured manner.

> db.customer_details.find()
{ "_id" : ObjectId("5e4d7e4264ffdc9f7c4129f8"), "name" : "foo" }
{ "_id" : ObjectId("5e4d7f2564ffdc9f7c4129f9"), "name" : "john", "place" : "ind", "status" : "active" }
{ "_id" : ObjectId("5e4d8116b3322fea377c0446"), "name" : "jay", "place" : "ind", "status" : "active" }
{ "_id" : ObjectId("5e4d8116b3322fea377c0447"), "name" : "peter", "place" : "canada", "status" : "active" }
{ "_id" : ObjectId("5e4d8116b3322fea377c0448"), "name" : "yuvi", "place" : "usa", "status" : "inactive" }

The pretty method will display the document in a structured manner.
Syntax:

db.COLLECTION_NAME.find().pretty()

Query:

> db.customer_details.find().pretty()
{ "_id" : ObjectId("5e4d7e4264ffdc9f7c4129f8"), "name" : "foo" }
{
"_id" : ObjectId("5e4d7f2564ffdc9f7c4129f9"),
"name" : "john",
"place" : "ind",
"status" : "active"
}
{
"_id" : ObjectId("5e4d8116b3322fea377c0446"),
"name" : "jay",
"place" : "ind",
"status" : "active"
}
{
"_id" : ObjectId("5e4d8116b3322fea377c0447"),
"name" : "peter",
"place" : "canada",
"status" : "active"
}
{
"_id" : ObjectId("5e4d8116b3322fea377c0448"),
"name" : "yuvi",
"place" : "usa",
"status" : "inactive"
}

Update document

MongoDB has an update() and save() method to update the document.

Update method

This update method will update the existing data.
Syntax:

db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATE_DATA)

I’m updating the status from active to inactive.
Query:

db.customer_details.update({"name" : "john", "place" : "ind", "status" : "active"},
{$set:{"name" : "john", "place" : "india", "status" : "inactive"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Updated document.
> db.customer_details.find().pretty()
{ "_id" : ObjectId("5e4d7e4264ffdc9f7c4129f8"), "name" : "foo" }
{
"_id" : ObjectId("5e4d7f2564ffdc9f7c4129f9"),
"name" : "john",
"place" : "india",
"status" : "inactive"
}
{
"_id" : ObjectId("5e4d8116b3322fea377c0446"),
"name" : "jay",
"place" : "ind",
"status" : "active"
}
{
"_id" : ObjectId("5e4d8116b3322fea377c0447"),
"name" : "peter",
"place" : "canada",
"status" : "active"
}
{
"_id" : ObjectId("5e4d8116b3322fea377c0448"),
"name" : "yuvi",
"place" : "usa",
"status" : "inactive"
}

By default, MongoDB will update only a single document. To update multiple documents, you need to set a parameter ‘multi’ to true.
Syntax:

db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATE_DATA,{multi:true})

Save method

This method replaces the existing data with the new data passed in the save() method.
Syntax:

db.COLLECTION_NAME.save({_id:ObjectId(), NEW_DATA})

Query:

> db.customer_details.save(
{
"_id" : ObjectId("5e68e77455d4514eaa05c458"),
"name":"john","by":"jay"
}
)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Replaced document

> db.customer_details.find().pretty()
{ "_id" : ObjectId("5e68e76155d4514eaa05c454"), "name" : "foo" }
{
"_id" : ObjectId("5e68e76a55d4514eaa05c455"),
"name" : "john",
"place" : "ind",
"status" : "active"
}
{
"_id" : ObjectId("5e68e77455d4514eaa05c456"),
"name" : "jay",
"place" : "ind",
"status" : "active"
}
{
"_id" : ObjectId("5e68e77455d4514eaa05c457"),
"name" : "peter",
"place" : "canada",
"status" : "active"
}
{
"_id" : ObjectId("5e68e77455d4514eaa05c458"),
"name" : "john",
"by" : "jay"
}

Delete document

MongoDB has a remove() method which is used to delete the document from the collection. The remove() has two acceptance criteria. They are Deletion criteria and JustOne.
Syntax:

db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

Query:

> db.customer_details.remove({"name" : "peter", "place" : "canada", "status" : "active"})
WriteResult({ "nRemoved" : 1 })

Deleted document

> db.customer_details.find().pretty()
{ "_id" : ObjectId("5e4d7e4264ffdc9f7c4129f8"), "name" : "foo" }
{
"_id" : ObjectId("5e4d7f2564ffdc9f7c4129f9"),
"name" : "john",
"place" : "india",
"status" : "inactive"
}
{
"_id" : ObjectId("5e4d8116b3322fea377c0446"),
"name" : "jay",
"place" : "ind",
"status" : "active"
}
{
"_id" : ObjectId("5e4d8116b3322fea377c0448"),
"name" : "yuvi",
"place" : "usa",
"status" : "inactive"
}

To remove all documents
Syntax:

db.COLLECTION_NAME.remove({})

Query:

> db.customer_details.remove({})
WriteResult({ "nRemoved" : 4 })
> db.customer_details.find()

if you want to learn more about MongoDB, you can refer to our previous MongoDB blogs such as MongoDB Vs Firebase. Which is better for your business or 10 tips to improve your MongoDB security.
If you have a business idea in your mind and in search of a reliable web development company, you are in the right place. Hire the best web developers in the industry from Agira technologies.

Looking for a Tech partner to dominate the digital world?