MongoDB Tutorial for Beginners: Learn MongoDB in Simple Steps
Updated on Feb 24, 2025 | 12 min read | 899.1k views
Share:
For working professionals
For fresh graduates
More
Updated on Feb 24, 2025 | 12 min read | 899.1k views
Share:
Table of Contents
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 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.
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:
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.
To create a new database in MongoDB, use the use command followed by the database name.
For example:
use mydatabase
Switch to the database and use the db.dropDatabase() command to drop a database.
For example:
use mydatabase
db.dropDatabase()
To create a new collection in MongoDB, use the DB.createCollection() command.
For example:
db.createCollection(“mycollection”)
To drop a collection, use the db. collection.drop() command.
For example:
db.mycollection.drop()
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 })
To query documents from a collection, use the db.collection.find() command.
For example:
db.mycollection.find({ age: { $gt: 25 } })
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 } })
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 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 })
To limit the number of records returned in a query, use the DB.collection.find().limit() command.
For example:
db.mycollection.find().limit(10)
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 })
Indexes in MongoDB improve query performance. Use the db.collection.createIndex() command to create indexes.
For example:
db.mycollection.createIndex({ name: 1 })
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’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.
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.
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.
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:
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.
For PHP programmers, MongoDB offers the MongoDB PHP Library. With the help of this library, PHP programmes may easily and clearly interface with MongoDB.
Several cutting-edge principles that MongoDB offers can improve your data management and querying abilities. Let’s explore a few of these ideas:
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 allow you to refer to documents in another collection. They provide a way to represent relationships between documents.
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.
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 ensure that a series of operations on a document is executed as a single, indivisible unit. MongoDB supports atomic operations at the document level.
MongoDB supports various indexes, including single-field, compound, geospatial, and text indexes. Choose the appropriate index type to optimize query performance.
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 is a unique identifier assigned to each document in MongoDB. It consists of a timestamp, machine identifier, process identifier, and a random value.
In MongoDB, MapReduce is a potent data processing method. You may use it to carry out intricate data aggregations and transformations across huge databases.
Full-text search capabilities are offered by MongoDB, allowing you to quickly do text-based searches on text fields.
Regular expressions can be used in MongoDB queries to match patterns in string fields. This offers functionally flexible search.
Rockmongo is a web-based MongoDB administration tool with a user-friendly interface for managing databases, collections, and documents.
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 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.
MongoDB does not provide auto-increment functionality by default. However, you can implement an auto-increment sequence manually using counters or other techniques.
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.
The usability of this data-oriented database depends on certain integral features of Mongo DB:-
Also, Check out our data science course to upskill yourself.
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 |
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.
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.
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:-
> use my_database;
> show collections;
users
posts
>
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
my_database 0.004GB
>
> db.users.findOne()
{
“_id”: ObjectId(“5ce45d7606444f199acfba1e”),
“name”: {given: “Anna”, family: “Smith”},
“email”: “email@example.com”
“age”: 36
}
>
> use my_database;
> db.users.count()
20234
>
> db.users.findOne({_id: ObjectId(“5ce45d7606444f199acfba1e”)})
{
“_id”: ObjectId(“5ce45d7606444f199acfba1e”),
“name”: {given: “Anna”, family: “Smith”},
“email”: “email@example.com”,
“age”: 36
}
>
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:
> db.users.find().limit(10)
…
>
> db.users.find({“name.family”: “Smith”}).count()
1
>
Note that we enclose “name.family” in quotes, because it has a dot in the middle.
// 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}})
// 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
}
>
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.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources