Home / Blogs / mongodb

Understanding MongoDB: Beginner Guide, Commands, and Cheatsheet to NoSQL Databases & CRUD Operations

Published on May 4, 2024
By Soumik Ghosh
Understanding MongoDB: Beginner Guide, Commands, and Cheatsheet to NoSQL Databases & CRUD Operations

What kind of Database is it?

Databases can be categorized based on how they store and manage data. Unlike traditional databases like MySQL, which use a table format to store data, MongoDB uses a different approach where data is stored in a JSON-like format. This type of database is known as a NoSQL database.

SQL-based databases (such as MySQL, Oracle SQL, etc.) are table-based databases where each row represents a new record and each column represents a specific attribute of the data. The tables are organized in a way that columns are related to each other, forming a structured, relational schema.

NoSQL databases (such as MongoDB, HBase, etc.) are document-oriented databases where each document is a new record, and they are stored in a manner as in parking cars beside each other, allowing for more flexible and scalable data models.

More About MongoDB

MongoDB is a NoSQL document based database which stores data in JSON like format called BSON (BSON stores data in a binary format, it also has support for data-type like date, and numeric specific numeric types). In MongoDB, database contains collections, each collection stores documents and each document is a set of key value pair. Values can be nested documents or arrays. This structure allows flexible and schema-less data modeling.

Here is a representation of MongoDB document:

{
	_id: ObjectId("5eb3d66831de5d88f4305b"),
	name: "Soumik",
	age: 22
}

MongoDB Database Commands

Show all databases:

show dbs

Create a new database/ Select an existing database:

use database_name

Check which database you are in:

db

Managing Collections in MongoDB

Show all collections:

show collections

View all data from the collection:

db.collection_name.find()

Create collection:

db.createCollection("new_collection")

Insert Document

To insert a single document in a collection:

db.collection_name.insertOne( { name: "soumik", age: 22 } )

(you should see output as acknowledged: true and an ObjectId will be returned)

To insert multiple document:

db.collection_name.insertMany( [ {name: "smik", age: 22}, {name: "xyz", age: 21} ] )

(like insertOne you will get an output acknowledged as true and two ObectId will be returned for two document)

How to Find and Query Documents in MongoDB

To see all the documents in a collection:

db.collection_name.find()

To see a single document in a collection:

db.collection_name.findOne()

Filter Data:

Show data based on one condition:

db.collection_name.find( { name: "soumik" } )

Show data based on multiple condition:

db.collection_name.find( { name: "soumik", age: 22 } )

Show only certain fields from the document:

db.collection_name.find( { name: 1, age: 0 } )

0 value will hide the certain field and 1 will show it

Now, sometimes the document might be embedded inside parent document, the structure will look something like this..

{
	_id: ObjectId("5eb3d66831de5d88f4305b"),
	name: "Soumik",
	age: 22
	address{
		street: "xyz street",
		building: 23,
		zipcode: 111111
	}
}

Here is an example of how to query an embedded document in MongoDB using dot notation:

db.collection_name.find( { "address.zipcode": 111111 } )

Comparison Operator:

$eq : values are equal

$ne : values aren't equal

$lt : left value is less than right value

$gt : left value is greater than right value

$lte : left value is less than equals to right value

$gte : left value is greater than equals to right value

An example of comparison operator :

db.collection_name.find( { age: {$gt : 20} } )

This will show all the documents which has age greater than 20.

Sort results:

db.collection_name.find( { name: "soumik" } ). sort( { "age" : 1 } )

It will show result with people whose name is "soumik" and result will be sorted in ascending order based on age.

To sort result in descending order, instead of 1 change the value to -1 "age" : -1.

If we add more than one value inside sort() lets say, sort({ "age":1, "year": 1}) this will sort the result based on age and if both have same age, it will be sorted based on year.

Querying Arrays in MongoDB (Exact Match & Operators)

Data can be stored as an array in document too, the data representation look like this:

{
	_id: ObjectId("5eb3d66831de5d88f4305b"),
	name: "Soumik",
	age: 22
	hobbies: ["drawing", "photography"]
}

Exact array match:

db.collection_name.find({ hobbies: ["drawing"] })

The output of the query will be the document where someone has only "drawing" as hobbies.

{
	_id: ObjectId("5eb3d66831de5d88f4305b"),
	name: "Soumik",
	age: 22
	hobbies: ["drawing"]
}

Single array element match:

db.collection_name.find({ hobbies: "drawing" })

The output will show all the documents that has "drawing" as its value, it can have more one value but it must contain "drawing" in it.

{
	_id: ObjectId("5eb3d66831de5d88f4305b"),
	name: "Soumik",
	age: 22
	hobbies: ["drawing", "photography"]
}
{
	_id: ObjectId("663e1063779dd1c9a6fa6079"),
	name: "Smik",
	age: 22
	hobbies: ["drawing"]
}

Array operator:

$all : it will match all the query array elements inside $all with document elements.

$elemMatch : It will search for at least one array element that satisfy the condition inside $elemMatch

$in : it searches the document array for any element that matches with the query element

Here is an example using all array operator:

db.student.find({ hobbies: { $all: ["drawing", "photography"] } })

[
  {
    _id: ObjectId("663e1063779dd1c9a6fa6078"),
    name: 'soumik',
    age: 22,
    hobbies: ['drawing', 'photography']
  }
]

db.student.find({ hobbies: { $elemMatch: { $eq: "drawing" } } })

[
  {
    _id: ObjectId("663e1063779dd1c9a6fa6078"),
    name: 'soumik',
    age: 22,
    hobbies: ['drawing', 'photography']
  },
  {
    _id: ObjectId("663e1063779dd1c9a6fa6079"),
    name: 'smik',
    age: 22,
    hobbies: ['drawing']
  }
]

db.student.find({ hobbies: { $in: ["photography", "drawing"] } })

[
  {
    _id: ObjectId("663e1063779dd1c9a6fa6078"),
    name: 'soumik',
    age: 22,
    hobbies: ['drawing', 'photography']
  },
  {
    _id: ObjectId("663e1063779dd1c9a6fa6079"),
    name: 'smik',
    age: 22,
    hobbies: ['drawing']
  }
]

Array fields with embedded documents:

The representation of this type of document is embedded documents are kept inside array documents.

{
	_id: ObjectId("663e1063779dd1c9a6fa6078"),
	name: "soumik",
	age: 22
	hobbies: ["drawing", "photography"],
	exp: [{
		company: "xyz",
		year: 2
	},
	{
		company: "abc",
		year: 3
	}	
	]
}

Here as you can see the exp is an array embedded document where an document is embedded inside an array.

Update a Document

To update a single document we use this query:

db.collection_name.updateOne({ name: "soumik" }, {$set: {age: 91}})

filter : first we filter out our result which we will change, in our case name: "soumik"

update : then we use the $set operator then update the previous value, like $set: {age: 91}

options : we can specify some options here if we want, if we want to create a new document if it doesn't exist we can use {upsert: true}

To update many document we use this query:

db.collection_name.updateMany({ name: "soumik" }, {$set: {age: 91, name: "oldie"}})

In this case, we filtered out the result by name then we modified every document which has name: "soumik" and change the age to 91 and name to "oldie".

To update embedded document:

db.collection_name.updateOne({ name: "soumik" }, {$set: { "parent.child": 1 }})

To update array document:

db.collection_name.updateOne({ name: "soumik" }, {$set: { "exp.0.year": 1 }})

In this query we have updated embedded array's document with 0th index year value to 1.

Append new document inside embedded array document:

Let's say we want to append another document array inside embedded array document then we can use this query

db.collection_name.updateOne({name: "soumik"}, {$push: {exp: {company: "lmao", year: 9}}})

Upserting a document which mean either update or insert a document:

We will add another document and if it's not present instead of update it will insert the document in it. Let's try to add another data inside our document

db.collection_name.updateOne({name: "soumik"}, {$set: {email: "xyz12@gmail.com"}}, {upsert: true})

Modify a document

We can use the findAndModify() query to find and update document. The following query will be

db.collection_name.findAndModify({query: {name: "soumik"}, update:{age: 91}, new: true})

The query can have four parameters:

query will filter out the document that matches the query

update will contain the updated document data

new will show the new updated document after modifying

upsert will update the old document if exists else it will insert a new document

Delete a Document

To delete one single document from the collection, we use the deleteOne()

db.collection_name.deleteOne({ name: "soumik" })

To delete more than one or let's say delete document in bulk, we can use the deleteMany() query.

db.collection_name.deleteMany({ name: "soumik" })

Replace the document with new document:

It replaces existing document and inserts new document inside it.

db.collection_name.replaceOne({ name: "soumik" }, {name: "lmao", age: 91})

We can use another parameter upsert as true to insert new data if it doesn't exist.

Indexing in Document

Indexing in MongoDB works as labeling documents based on similar data. Or you can think of it as index page of a book, it separates similar data in a place so that you don't have to read/search the whole book to find the specific data.

Main purpose of indexing is to improve performance of searching a document by labeling data so that we dont have to search the whole big document.

Here is a visual representation of indexing in MongoDB:

Blog Image

We have one collection called student where student where all the students are present with age value, now we made a index where only 12 year olds are separated, so now if we want to find someone whose age is 12, we dont have search the whole student document. This might not look like a big difference but for large scale data it's efficient.

Add Index

Query to add indexing:

db.collection_name.createIndex({age: 1})

Here age: 1 means it will sort the index in ascending order, and if we write age: -1 it will sort it in descending order.

Compound Index:

db.collection_name.createIndex({age: 1, name: -1})

This will create index which will sort age in ascending order and name in descending order.

Delete Index

To delete index you can use this query:

db.collection_name.dropIndex( "age_1" )

This will delete the index which was storing the age index named age_1

Aggregation

Aggregate means to combine several filtering or sorting methods to get a result.

Aggregation pipelines:

Aggregation pipeline is like sending document from end of pipe to other end but inside pipe there are segments each segment performs certain operations on the pipe like sorting or filtering data.

To use aggregate using pipelines we have to use the .aggregate() method

db.collecion_name.aggregate()

Match our result in a stage:

As we came to know there are many stages in a pipeline, there is a simple $match method which works as .find() method.

db.collection_name.aggregate([ { $match: {name: "soumik"} } ])

Sort our results on next stage:

db.collection_name.aggregate([ { $match: {name: "soumik"} }, { $sort: {age: 1} } ])

We can also add new fields in our document using$addFields:

db.collection_name.aggregate([ { $match: {name: "soumik"}}, { $addFields: {totalExp: {$sum: { "$exp.year" }}}} ])

This will add a new field called totalExp to our document which will add the years inside exp.year and sum it up.

Create new collection using $out method:

db.collection_name.aggregate([ { $match: {age: 22}}, {$out: "age_22"} ])

It will make a new collection in our database which matches the age 22 and create a new collection storing document who is 22 years old.

Table comparing SQL terminology to MongoDB terminology:

SQL TermMongoDB Term
DatabaseDatabase
TableCollection
Row Document
Column Field
Join$lookup / Embedding

Frequently Asked Questions

Is MongoDB good for large-scale applications?

Yes, MongoDB can be used for large-scale applications. Its document-based model, indexing, replication, and horizontal scaling make it suitable for applications that need to handle large amounts of data.

But MongoDB being a NoSQL database doesn't automatically mean it will be fast at any scale. Your schema design and indexes still matter a lot. You should design the database around how your application actually reads and writes data instead of just storing everything in a flexible document.

How do I improve MongoDB query performance?

The first thing you should look at is indexing. If you frequently search or sort documents using a particular field, creating an appropriate index can prevent MongoDB from scanning every document in the collection.

However, adding indexes everywhere isn't a good solution either. Indexes also consume storage and can make writes more expensive. It's better to look at the queries your application actually uses and create indexes around those query patterns.

For larger applications, schema design and the aggregation pipeline also become important when optimizing MongoDB performance.

Should I embed or reference documents in MongoDB?

It depends on how the data is used. MongoDB lets you store related data inside the same document using embedded documents, or keep it separately and reference it when needed.

Embedding can be useful when related data is usually accessed together. For example, storing a user's address inside the user document can make sense if you almost always need the address whenever you fetch the user.

Referencing can make more sense when the related data is large, frequently updated independently, or shared by multiple documents.

There isn't one rule that says embedding is always better than referencing. Your application's query patterns should decide how you structure the data. MongoDB itself recommends starting with your workload and relationships when designing a schema.

Is MongoDB better than MySQL?

MongoDB isn't simply better than MySQL. They solve database problems using different approaches.

MongoDB is a document-oriented NoSQL database, so it works well when your data is naturally represented as flexible documents and your application benefits from that model. MySQL is a relational database, where data is organized into tables and relationships are explicitly defined.

If your application has highly structured relational data and depends heavily on joins and strong relational constraints, MySQL can be a better choice. If your data is more flexible or naturally fits a document structure, MongoDB can be a good option.

The important thing is to choose the database based on your application's data and query patterns rather than choosing MongoDB just because it is a NoSQL database.

Subscribe to our newsteller to stay updated with our latest blogs.