Programs

MongoDB Tutorial for Beginners: Learn MongoDB in Simple Steps

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

>

Explore our Popular Data Science Courses

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

}

>

Top Data Science Skills to Learn

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. 

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).

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.

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.

Want to share this article?

Be Certified Data Engineer

Leave a comment

Your email address will not be published. Required fields are marked *

Our Trending Cloud Computing Courses

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks