Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

MongoDB Tutorial for Beginners: Learn MongoDB in Simple Steps

Updated on 28 June, 2023

898.99K+ views
12 min read

MongoDB is a document database and a leading NoSQL database that is open-sourced. It is written in C++ and is a document-oriented NoSQL database used for high-volume data storage, content management and delivery, mobile and social infrastructures, user data management, etc.

It consists of a data model using which you can represent hierarchical relationships. It relies on JSON-like documents with optional schema and does not use rows and tables as used in traditional relational databases. The documents with key-value pairs are the fundamental units of data in MongoDB.

MongoDB stores data in JSON format. Its high demand is mainly based on its index on any attribute, replication and high availability, auto-sharding, rich queries, quick in-place updates, and rich community support. Let’s dive into mongodb query tutorial. 

MongoDB – Advantages

MongoDB is a powerful and flexible NoSQL database that has recently gained significant popularity. It offers numerous advantages over traditional relational databases, making it an excellent choice for many applications.

The adaptable data model of MongoDB is one of its main benefits. Contrary to relational databases, MongoDB enables document-based data storage, making it simple to store complex structures and nested data. Due to its flexibility, MongoDB is the best database for handling dynamic and changing data.

Scalability is an additional benefit. Because MongoDB is made to grow horizontally, you may effectively disperse your data over numerous servers and deal with high traffic volumes. Additionally, it offers automatic sharding, which enables you to easily partition your data and distribute it across other machines.

MongoDB – Environment

To get started with MongoDB, you need to set up your environment. Here’s how to learn MongoDB step by step, install and configure MongoDB:

  • The MongoDB Community Server can be downloaded from the official website.
  • Refer to the installation guidelines for your operating system.
  • The MongoDB environment variables should be set up.
  • The MongoDB server should be started.
  • Run MongoDB commands in the terminal to check the installation.

MongoDB – Data Modeling

A critical component of MongoDB development is data modelling. Data is kept in MongoDB in flexible JSON-like documents known as BSONs (Binary JSON). Several important factors for data modelling in MongoDB are listed below:

Denormalization: MongoDB promotes denormalisation by integrating relevant data into a single document to enhance query performance.

One-to-One Relationships: You can embed the pertinent data into the page for one-to-one relationships.

One-to-Many Relationships: Depending on the volume and type of the connected data, use the embedding or referencing technique.

Many-to-Many Relationships: Use a variety of references or embedding to implement many-to-many relationships.

MongoDB – Database Operations

Create Database

To create a new database in MongoDB, use the use command followed by the database name. 

For example:

use mydatabase

Drop Database

Switch to the database and use the db.dropDatabase() command to drop a database. 

For example:

use mydatabase

db.dropDatabase()

Create Collection

To create a new collection in MongoDB, use the DB.createCollection() command. 

For example:

db.createCollection(“mycollection”)

Drop Collection

To drop a collection, use the db. collection.drop() command. 

For example:

db.mycollection.drop()

Insert Document

To insert a document into a collection, use the db.collection.insertOne() or db.collection.insertMany() command. 

For example:

db.mycollection.insertOne({ name: “John Doe”, age: 30 })

Query Document

To query documents from a collection, use the db.collection.find() command. 

For example:

db.mycollection.find({ age: { $gt: 25 } })

Update Document

To update a document in a collection, use the db.collection.updateOne() or db.collection.updateMany() command. 

For example:

db.mycollection.updateOne({ name: “John Doe” }, { $set: { age: 35 } })

Delete Document

To delete a document from a collection, use the db.collection.deleteOne() or db.collection.deleteMany() command. 

For example:

db.mycollection.deleteOne({ name: “John Doe” })

Projection

Projection allows you to retrieve only specific fields from a document. Use the db.collection.find() command with the projection parameter. 

For example:

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

Limiting Records

To limit the number of records returned in a query, use the DB.collection.find().limit() command. 

For example:

db.mycollection.find().limit(10)

Sorting Records

To sort the query results in ascending or descending order, use the DB.collection.find().sort() command. 

For example:

db.mycollection.find().sort({ age: 1 })

Indexing

Indexes in MongoDB improve query performance. Use the db.collection.createIndex() command to create indexes. 

For example:

db.mycollection.createIndex({ name: 1 })

Aggregation

Aggregation in MongoDB allows you to perform advanced data analysis operations. Use the db.collection.aggregate() command. 

For example:

db.mycollection.aggregate([
  { $group: { _id: “$category”, total: { $sum: “$quantity” } } }
])

MongoDB – Replication

MongoDB’s replication feature ensures high availability and fault tolerance by keeping several copies of the data on many servers. It guarantees that the data is still available even if one server fails. In the primary-secondary replication paradigm used by MongoDB, one server serves as the primary and the other servers serve as the secondary.

You must configure a replica set by identifying its members (servers) and their roles before you can set up replication.

MongoDB – Sharding

In MongoDB, sharding allows for horizontal scaling by dividing data among numerous computers or shards. A unified picture of the complete dataset is provided by each shard, each of which maintains a portion of the data.

You must configure a sharded cluster with several shards, config servers, and Mongo routers in order to set up sharding.

MongoDB – Backup and Deployment

In order to ensure data security, MongoDB must have a backup and deployment plan. MongoDB provides point-in-time, comprehensive, and incremental backups among other backup techniques.

You may use tools like Mongodump and mongorestore to create backups and restore them.

MongoDB with Programming Languages

By providing official drivers for several computer languages, MongoDB makes it simple to integrate MongoDB with your preferred language. Let’s look at two popular programming languages and their compatibility with MongoDB:

MongoDB with Java

To connect MongoDB to Java, use the MongoDB Java Driver. This driver makes it straightforward to connect to MongoDB, perform CRUD tasks, and execute sophisticated queries.

MongoDB with PHP

For PHP programmers, MongoDB offers the MongoDB PHP Library. With the help of this library, PHP programmes may easily and clearly interface with MongoDB.

Advanced MongoDB Concepts

Several cutting-edge principles that MongoDB offers can improve your data management and querying abilities. Let’s explore a few of these ideas:

Relationships

Relationships between documents in MongoDB can be created using embedding or references. Select the best strategy depending on the type and volume of the relevant data.

Database References

Database references allow you to refer to documents in another collection. They provide a way to represent relationships between documents.

Covered Queries

A covered query is a query where all the requested fields are in an index. Covered queries can significantly improve performance by avoiding disk access to retrieve documents.

Analyzing Queries

MongoDB provides tools like the Explain Plan to analyze the query execution plan and identify performance bottlenecks. Analyzing queries helps optimize query performance.

Atomic Operations

Atomic operations ensure that a series of operations on a document is executed as a single, indivisible unit. MongoDB supports atomic operations at the document level.

Advanced Indexing

MongoDB supports various indexes, including single-field, compound, geospatial, and text indexes. Choose the appropriate index type to optimize query performance.

Indexing Limitations

While indexes improve performance, they also have limitations. Indexes require additional storage space and can slow down write operations. Be mindful of these trade-offs when designing your database.

ObjectId

ObjectId is a unique identifier assigned to each document in MongoDB. It consists of a timestamp, machine identifier, process identifier, and a random value.

Map Reduce

In MongoDB, MapReduce is a potent data processing method. You may use it to carry out intricate data aggregations and transformations across huge databases.

Text Search

Full-text search capabilities are offered by MongoDB, allowing you to quickly do text-based searches on text fields.

Regular Expression

Regular expressions can be used in MongoDB queries to match patterns in string fields. This offers functionally flexible search.

Additional MongoDB Topics

Working with Rockmongo

Rockmongo is a web-based MongoDB administration tool with a user-friendly interface for managing databases, collections, and documents.

GridFS

GridFS is a specification in MongoDB for storing and retrieving large files, exceeding the BSON document size limit of 16 MB. It splits files into smaller chunks and stores them as separate documents.

Capped Collections

Capped collections are fixed-size collections that maintain the insertion order. Once the collection reaches its size limit, the oldest documents are automatically removed to accommodate new ones.

Auto-Increment Sequence

MongoDB does not provide auto-increment functionality by default. However, you can implement an auto-increment sequence manually using counters or other techniques.

MongoDB Useful Resources

Refer to the following resources to further your knowledge about MongoDB and study more complex topics:

MongoDB Official Documentation: The official documentation provides in-depth information on MongoDB’s features, concepts, and best practices.

MongoDB University: MongoDB offers free online courses and certifications to help you master MongoDB development and administration.

Stack Overflow: A popular question-and-answer platform where you can find solutions to common MongoDB-related issues.

MongoDB Community: Engage with the community through forums, user groups, and events to gain insights and share experiences.

MongoDB Features

The usability of this data-oriented database depends on certain integral features of Mongo DB:-

  • Each database consists of collections that further contain documents. These documents and their size and content can differ according to different numbers of fields. 
  • The document structure is based on how developers build classes and objects in their programs. 
  • The rows don’t require a predefined schema, as you can create the fields on the go.
  • With the MongoDB data model, you can represent hierarchical relationships for storing arrays and other complex structures.
  • MongoDB environments are very scalable. 
  • Application objects need not be converted or mapped to database objects.
  • Internal memory stores the windowed working set that enables easier access to data.
  • The structure of a single object is clear.
  • There aren’t any complex joins.
  • Its deep query-ability supports dynamic queries on documents.

Also, Check out our data science course to upskill yourself.

Things You Need To Know About MongoDB and RDBMS

MongoDB is a document-oriented, cross-platform database that delivers high performance, effective scalability, and high availability. It works based on Document and Collection. Every database in MongoDB has its distinct set of files, and ideally, a single MongoDB server has numerous databases. 

On the other hand, a Collection is a group of MongoDB documents equivalent to the RDBMS table. It exists inside a single database and does not enforce a schema. The Documents within a collection can have various fields, and all documents inside a collection are of related or similar purpose. A set of key-value pairs are called Documents having a dynamic schema, wherein the documents in the same collection need not have the same set of structure or fields. 

You will find the relationship of RDBMS terminology with MongoDB in the following table.

RDBMS MongoDB
Database Database
Tuple Document
Table Collection
Table join Embedded documents
column Field
Primary Key Primary Key (MongoDB offers the Default key _id)
Database Server and Client
mysqld/Oracle mongod
mysql/sqlplus mongo

Difference Between MongoDB and SQL databases

MongoDB is more flexible than SQL and offers better data availability. It is also portable and extendable. SQL databases are known for their durability and consistency. Thus, the best database solution depends on the kind of project one is involved in. 

MongoDB is better for development pipelines, while SQL databases are great for reliable organisational data transactions. MongoDB is great for processing and working with unstructured data. This NoSQL database is one of the best alternatives for feeding data pipelines and for applications, such as real-time analytics.

MongoDB is great for working with IoT devices and mobile/web applications. It is fast becoming the preferred choice for developers worldwide due to its flexibility and ability to scale up smoothly.

While SQL databases are great for working with structured data, Data Science and for advanced organizational tasks, MongoDB satisfies the requirements of modern developers and businesses through the effective development of software or online services.

Examples Of How To Use MongoDB 

Storing Nested Data Structures

The ability to nest objects within documents is one of the greatest features of MongoDB. To embed data inside documents, developers structure data in MongoDB rather than breaking it apart into various collections. 

Here is an example:-

{_id: ObjectId(“5effaa5662679b5af2c58829”),
 email: “email@example.com”,
 name: {given: “Lily”, family: “Mona”},
 age: 31,
 addresses: [{label: “home”,
           street: “22 1b Baker Street”,
           city: “London”,
           state: “ENG”,
           zip: “NW1 6XE”,
           country: “UK”},
          {label: “mom”,
           street: “555 Park Street”,
           city: “Lake Town”,
           province: “Ontario”,
           country: “CA”}]
           }

The name field is the nested object having given and family name components in the addresses field where multiple addresses are stored in one array. Each address can have various fields making it easy to store other data types.

Using the MongoDB Shell

The MongoDB shell is mainly used for navigating, manipulating and inspecting document data. When MongoDB is run on a local machine, firing up the shell connects to MongoDB at localhost on the standard port. Make sure to add the connection string after command mongo if you need to connect to a MongoDB Atlas cluster or other remote instance.

You will find quick shell examples below:-

List Collections

> use my_database;

> show collections;

users

posts

>

List Databases

> show dbs;

admin 0.000GB

config   0.000GB

local 0.000GB

my_database  0.004GB

>

Find the First Document in a Collection

> db.users.findOne()

{

“_id”: ObjectId(“5ce45d7606444f199acfba1e”),

“name”: {given: “Anna”, family: “Smith”},

“email”: “email@example.com”

“age”: 36

}

>

Count Documents in a Collection

> use my_database;

> db.users.count()

20234

>

Find a Document by ID

> db.users.findOne({_id: ObjectId(“5ce45d7606444f199acfba1e”)})

{

“_id”: ObjectId(“5ce45d7606444f199acfba1e”),

“name”: {given: “Anna”, family: “Smith”},

“email”: “email@example.com”,

“age”: 36

}

>

Querying MongoDB Collections

The same syntax in the MongoDB Query Language (MQL) is used in the documents that can be used in advanced querying. You will find the MongoDB query examples below:

Find a Limited Number of Results

> db.users.find().limit(10)
…
>

Find Users by Family name

> db.users.find({“name.family”: “Smith”}).count()
1
> 

Note that we enclose “name.family” in quotes, because it has a dot in the middle.

Query Documents by Numeric Ranges

// All posts having “likes” field with numeric value greater than one:

> db.post.find({likes: {$gt: 1}})

// All posts having 0 likes

> db.post.find({likes: 0})

// All posts that do NOT have exactly 1 like

> db.post.find({likes: {$ne: 1}})

Sort Results by a Field

// order by age, in ascending order (smallest values first)

> db.user.find().sort({age: 1})
{
    “_id”: ObjectId(“5ce45d7606444f199acfba1e”),
    “name”: {given: “Alex”, family: “Smith”},
    “email”: “email@example.com”,
    “age”: 27
}
{
    _id: ObjectId(“5effaa5662679b5af2c58829”),
    email: “email@example.com”,
    name: {given: “Jesse”, family: “Xiao”},
    age: 31
}
>
// order by age, in descending order (largest values first)
> db.user.find().sort({age: -1})
{
    _id: ObjectId(“5effaa5662679b5af2c58829”),
    email: “email@example.com”,
    name: {given: “Lilly”, family: “Mona”},
    age: 31
}
{
    “_id”: ObjectId(“5ce45d7606444f199acfba1e”),
    “name”: {given: “Anna”, family: “Smith”},
    “email”: “email@example.com”,
    “age”: 36
}
>

Conclusion

MongoDB’s scalability makes it one of the most extensively used databases because it provides a number of benefits in the field of software development and data science. Pursuing a career in these fields is a lucrative option. upGrad’s Executive PG Programme in Data Science is a great place to kickstart your career in data science and other related fields. 

Frequently Asked Questions (FAQs)

1. Why is MongoDB so easy to scale?

MongoDB is easy to scale because it is not an SQL database, as the data inside MongoDB is not coupled relationally. Data in MongoDB is stored in a self-contained format which enables easy horizontal scaling by distributing these documents across multiple systems (nodes).

2. What is the difference between MongoDB and database systems such as MariaDB and MySQL?

MongoDB is a NoSQL database while MariaDB and MySQL are SQL databases. Also, MongoDB is a Non-relational database while the other two are relational databases.

3. Why is MongoDB so popular?

MongoDB is an essential part of the MERN stack in web development. It is also easy to deploy and scale projects on MongoDB. MongoDB is known for its scalability. It is a great fit for any development pipeline.