codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

1 / 65 topics
1Introduction to MongoDB2MongoDB Architecture3Installation and Setup4Connecting to MongoDB5MongoDB Shell Basics
Tutorials/MongoDB/Introduction to MongoDB
🍃MongoDB

Introduction to MongoDB

Updated 2026-04-20
3 min read

Introduction to MongoDB

MongoDB is a popular NoSQL database that provides high performance, high availability, and easy scalability. It's designed to handle large volumes of data across many servers, making it an excellent choice for modern web applications.

What is MongoDB?

MongoDB is a document-oriented NoSQL database. Unlike traditional relational databases like MySQL or PostgreSQL, which use tables with rows and columns, MongoDB stores data in JSON-like documents. This makes it highly flexible and easy to work with complex data structures.

Key Features of MongoDB

  • Document-Oriented: Data is stored as documents in a format similar to JSON.
  • Scalable: Easily scales horizontally by adding more servers.
  • High Availability: Provides automatic failover and replica sets for high availability.
  • Flexible Schema: Allows dynamic schema changes without downtime.

Setting Up MongoDB

Installation

MongoDB can be installed on various operating systems. Here are the steps to install it on Ubuntu:

  1. Import the public key used by the package management system:

    wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
    
  2. Create a list file for MongoDB:

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
    
  3. Reload the package database:

    sudo apt-get update
    
  4. Install MongoDB packages:

    sudo apt-get install -y mongodb-org
    
  5. Start the MongoDB service:

    sudo systemctl start mongod
    
  6. Enable MongoDB to start on boot:

    sudo systemctl enable mongod
    

Connecting to MongoDB

You can connect to MongoDB using the mongo shell:

mongo

This will open the MongoDB shell where you can interact with your database.

Basic Concepts in MongoDB

Databases

A database in MongoDB is a container for collections. You can switch between databases using the use command:

use myDatabase

If the database does not exist, MongoDB will create it when you first store data in it.

Collections

Collections are similar to tables in relational databases. They store documents. Documents are JSON-like objects that can have different structures.

Documents

Documents are stored as BSON (Binary JSON) and can contain nested arrays and objects. Here is an example of a document:

{
  "name": "John Doe",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "Anytown"
  },
  "phoneNumbers": [
    { "type": "home", "number": "555-1234" },
    { "type": "work", "number": "555-5678" }
  ]
}

Basic Operations in MongoDB

Inserting Documents

To insert a document into a collection, use the insertOne or insertMany methods:

db.myCollection.insertOne({
  name: "John Doe",
  age: 30,
  address: {
    street: "123 Main St",
    city: "Anytown"
  }
})

To insert multiple documents:

db.myCollection.insertMany([
  { name: "Jane Doe", age: 25 },
  { name: "Jim Beam", age: 40 }
])

Querying Documents

You can query documents using the find method. Here are some examples:

  • Find all documents:

    db.myCollection.find()
    
  • Find documents with a specific field value:

    db.myCollection.find({ name: "John Doe" })
    
  • Use projection to specify which fields to return:

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

Updating Documents

To update documents, use the updateOne or updateMany methods:

  • Update a single document:

    db.myCollection.updateOne(
      { name: "John Doe" },
      { $set: { age: 31 } }
    )
    
  • Update multiple documents:

    db.myCollection.updateMany(
      { age: { $lt: 30 } },
      { $inc: { age: 1 } }
    )
    

Deleting Documents

To delete documents, use the deleteOne or deleteMany methods:

  • Delete a single document:

    db.myCollection.deleteOne({ name: "John Doe" })
    
  • Delete multiple documents:

    db.myCollection.deleteMany({ age: { $gt: 40 } })
    

Best Practices

  1. Indexing: Use indexes to improve query performance.
  2. Sharding: Distribute data across multiple servers for scalability.
  3. Replication: Use replica sets to ensure high availability.
  4. Security: Enable authentication and use role-based access control.

Indexing Example

To create an index on the name field:

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

Conclusion

MongoDB is a powerful NoSQL database that offers flexibility, scalability, and high performance. By understanding its basic concepts and operations, you can effectively use MongoDB to build modern web applications. This tutorial has covered the essentials of setting up MongoDB, performing CRUD operations, and implementing best practices.


Next MongoDB Architecture

Recommended Gear

MongoDB Architecture