Skip to content

manthanank/learn-mongodb

Repository files navigation

Learn MongoDB

This repository provides a comprehensive guide to help you get started with MongoDB, a popular NoSQL database. MongoDB is known for its flexibility, scalability, and ease of use, making it a preferred choice for many developers and organizations.

NPM Package Releases Npm Package total downloads Npm Package weekly downloads Npm Package monthly downloads Npm Package yearly downloads

Table of Contents

Introduction to MongoDB

MongoDB is a document-oriented NoSQL database, designed to store, query, and process large amounts of unstructured or semi-structured data. It uses a flexible, JSON-like document format called BSON (Binary JSON) to represent data.

Back to Top⤴️

Key Features of MongoDB

  • Schema-less: MongoDB does not require a predefined schema, allowing you to store data in a flexible manner.
  • High Performance: MongoDB provides high-speed read and write operations due to its efficient indexing and storage mechanisms.
  • Scalability: MongoDB supports horizontal scaling through sharding, enabling you to distribute data across multiple servers.

Use Cases of MongoDB

  • Content Management: MongoDB is suitable for managing content-heavy applications like blogs, news sites, and e-commerce platforms.
  • Real-time Analytics: MongoDB can handle real-time data processing and analytics for applications that require quick insights.
  • Internet of Things (IoT): MongoDB is used in IoT applications to store and process sensor data from connected devices.

Back to Top⤴️

Getting Started

To start using MongoDB, you need to install the MongoDB server on your local machine or use a cloud-based MongoDB service like MongoDB Atlas.

Installation

Follow the official MongoDB installation guide to set up MongoDB on your system.

Back to Top⤴️

Basic Concepts

  • Collections: Equivalent to tables in relational databases, collections store documents.
  • Documents: BSON data format records that store data in key-value pairs.
  • Fields: Key-value pairs within a document.
  • Indexes: Improve query performance by providing a quick access path to the data.

Back to Top⤴️

MongoDB Shell

MongoDB provides a command-line interface called the MongoDB shell to interact with the database. You can perform CRUD operations, create indexes, and run queries using the shell.

Back to Top⤴️

Basic Commands

  • show dbs: List all databases.
  • use dbName: Switch to a specific database.
  • show collections: List all collections in the current database.
  • db.collection.find(): Retrieve all documents from a collection.
  • db.collection.insertOne({ field: value }): Insert a document into a collection.

Back to Top⤴️

Data Modeling

MongoDB uses a flexible data model that allows you to represent complex hierarchical relationships easily. You can embed documents within documents or reference other documents using references.

Embedded Data Model

{
  "name": "John Doe",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "Cityville",
    "state": "CA",
    "zipCode": "12345"
  }
}

Referenced Data Model

{
  "name": "John Doe",
  "age": 30,
  "addressId": ObjectId("60a7b7b7e4b0c5f7c7b1e3a1")
}

Back to Top⤴️

Indexing

Indexes in MongoDB improve query performance by allowing the database to quickly locate and retrieve data. You can create indexes on single fields, compound fields, or text fields.

Creating an Index

db.users.createIndex({ name: 1 });

Compound Index

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

Text Index

db.articles.createIndex({ content: "text" });

Back to Top⤴️

Connecting to MongoDB

To connect to MongoDB from your application, you can use the MongoDB Node.js driver or an Object Data Modeling (ODM) library like Mongoose.

Using the MongoDB Node.js Driver

const { MongoClient } = require("mongodb");

const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);

async function connectToMongoDB() {
  try {
    await client.connect();
    console.log("Connected to MongoDB!");
  } catch (error) {
    console.error("Connection failed!", error);
  }
}

connectToMongoDB();

Using Mongoose

const mongoose = require("mongoose");

const uri = "mongodb://localhost:27017/mydatabase";

mongoose
  .connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log("Connected to MongoDB database!");
  })
  .catch((error) => {
    console.error("Connection failed!", error);
  });

Back to Top⤴️

CRUD Operations

Insert documents into a collection

// Insert a single document
db.users.insertOne({
  name: "John Doe",
  age: 25,
  email: "john@example.com"
});

// Insert multiple documents
db.users.insertMany([
  { name: "Jane Doe", age: 30, email: "jane@example.com" },
  { name: "Bob Smith", age: 22, email: "bob@example.com" }
]);

Query documents from a collection

// Find all documents in the users collection
db.users.find();

// Find a document by a specific field value
db.users.findOne({ name: "John Doe" });

Update documents in a collection

// Update a single document
db.users.updateOne(
  { name: "John Doe" },
  { $set: { age: 26 } }
);

// Update multiple documents
db.users.updateMany(
  { age: { $lt: 30 } },
  { $inc: { age: 1 } }
);

// Replace a single document
db.users.replaceOne(
  { name: "John Doe" },
  { name: "John Doe", age: 26, updated_at: new Date() }
);

Delete documents from a collection

// Delete a single document
db.users.deleteOne({ name: "Bob Smith" });

// Delete multiple documents
db.users.deleteMany({ age: { $gte: 30 } });

Back to Top⤴️

Database Commands

View all databases

show dbs

Create a new or switch databases

use dbName

View current Database

db

Delete Database

db.dropDatabase()

Back to Top⤴️

Collection Commands

Show Collections

show collections

Create a collection named 'comments’

db.createCollection('data')

Drop a collection named 'comments’

db.comments.drop()

Back to Top⤴️

Row(Document) Commands

Show all Rows in a Collection

db.comments.find()

Show all Rows in a Collection (Prettified)

db.comments.find().pretty()

Find the first row matching the object

db.comments.findOne({name: 'Manthan'})

Insert One Row

db.comments.insert({
    'firstName': 'Manthan',
    'lastName': 'Ank'
 })

Insert many Rows

db.comments.insertMany([{'firstName': 'Manthan', 'lastName': 'Ank'},{'firstName': 'Gagan','lastName': 'BA'}])

Search in a MongoDB Database

db.comments.find({lang:'JavaScript'})

Limit the number of rows in output

db.comments.find().limit(3)

Count the number of rows in the output

db.comments.find().count()

Update a row

db.comments.updateOne({name: 'Manthan'},{$set: {'name': 'Manthan','lang': 'JavaScript','mem_since': 1}},{upsert: true})

MongoDB Increment Operator

db.comments.update({name: 'Manthan'},{$inc:{mem_since: 2}})

MongoDB Rename Operator

db.comments.update({name: 'Manthan'},{$rename:{mem_since: 'member'}})

Delete Row

db.comments.remove({name: 'Manthan'})

Less than/Greater than/ Less than or Eq/Greater than or Eq

db.comments.find({member_since: {$lt: 90}})
db.comments.find({member_since: {$lte: 90}})
db.comments.find({member_since: {$gt: 90}})
db.comments.find({member_since: {$gte: 90}})

Back to Top⤴️

Aggregation Framework

MongoDB's Aggregation Framework is a powerful tool for data transformation and analysis. You can include examples of aggregation pipeline stages like $match, $group, $project, and others.

db.collection.aggregate([
   { $match: { field: value } },
   { $group: { _id: "$field", count: { $sum: 1 } } },
   { $project: { _id: 0, field: "$_id", count: 1 } }
])

Back to Top⤴️

Text Search

MongoDB supports full-text search capabilities. You can demonstrate how to perform text searches on a text index.

db.collection.createIndex({ fieldName: "text" })
db.collection.find({ $text: { $search: "searchQuery" } })

Back to Top⤴️

Geospatial Queries

MongoDB has built-in support for geospatial queries. You can showcase how to query documents based on their geographical location.

db.collection.createIndex({ locationField: "2dsphere" })
db.collection.find({
   locationField: {
      $near: {
         $geometry: { type: "Point", coordinates: [longitude, latitude] },
         $maxDistance: 1000  // in meters
      }
   }
})

Back to Top⤴️

Transactions

If you are using MongoDB version 4.0 or above, you can include examples of transactions for handling multiple operations atomically.

session = db.getMongo().startSession()
session.startTransaction()
try {
   // Perform multiple operations
   db.collection1.updateOne({ field: value1 }, { $set: { updateField1: newValue1 } })
   db.collection2.updateOne({ field: value2 }, { $set: { updateField2: newValue2 } })
   session.commitTransaction()
} catch (error) {
   print("Transaction failed. Aborting...")
   session.abortTransaction()
}
finally {
   session.endSession()
}

Back to Top⤴️

Data Validation

MongoDB 3.6 and later versions support JSON Schema validation. You can provide examples of how to enforce a schema on a collection.

db.createCollection("validatedCollection", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: ["field1", "field2"],
         properties: {
            field1: { bsonType: "string" },
            field2: { bsonType: "int" }
         }
      }
   }
})

Back to Top⤴️

Security

Briefly touch upon MongoDB security practices, such as authentication, authorization, and connection security.

# Creating a user with readWrite privileges
use admin
db.createUser({
   user: "username",
   pwd: "password",
   roles: [{ role: "readWrite", db: "databaseName" }]
})

Back to Top⤴️

Backup and Restore

Explain how to perform backups and restores using mongodump and mongorestore utilities.

# Backup a database
mongodump --db databaseName --out /path/to/backup

# Restore a database
mongorestore --db databaseName /path/to/backup/databaseName

Back to Top⤴️

MongoDB Atlas

Discover MongoDB Atlas, the cloud-based database service, and learn how to deploy, manage, and scale MongoDB clusters.

Back to Top⤴️

Interview Questions

  1. What is MongoDB? How is it different from SQL databases?

    • MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. Unlike SQL databases, it does not require a predefined schema and supports horizontal scaling.
  2. What are the key features of MongoDB?

    • Schema-less, high performance, scalability, flexible data model, rich query language, and support for replication and sharding.
  3. Explain the concept of a document and collection in MongoDB.

    • A document is a record in MongoDB, stored in BSON format. A collection is a group of documents, similar to a table in SQL databases.
  4. What is BSON in MongoDB?

    • BSON (Binary JSON) is a binary representation of JSON-like documents, used internally by MongoDB for data storage and transfer.
  5. How do you insert, update, and delete documents in MongoDB?

    • Insert: db.collection.insertOne({ ... })
    • Update: db.collection.updateOne({ ... }, { $set: { ... } })
    • Delete: db.collection.deleteOne({ ... })
  6. What is the difference between find() and findOne()?

    • find() retrieves all documents matching the query, while findOne() retrieves only the first matching document.
  7. What are indexes in MongoDB? How do they improve performance?

    • Indexes are special data structures that store a small portion of the collection's data set in an easy-to-traverse form. They improve query performance by allowing the database to quickly locate and retrieve data.
  8. What is the purpose of the _id field in MongoDB?

    • The _id field is a unique identifier for each document in a collection. It ensures that each document can be uniquely identified and retrieved.
  9. Explain the difference between db.collection.drop() and db.collection.remove().

    • db.collection.drop() deletes the entire collection, including all documents and indexes. db.collection.remove() deletes documents matching the query but keeps the collection and indexes.
  10. How do you create and drop databases in MongoDB?

    • Create: use dbName
    • Drop: db.dropDatabase()
  11. What is sharding in MongoDB? Why is it important?

    • Sharding is the process of distributing data across multiple servers to support horizontal scaling. It is important for handling large datasets and high-throughput applications.
  12. Explain the aggregation framework in MongoDB.

    • The aggregation framework is a powerful tool for data transformation and analysis. It uses a pipeline of stages, such as $match, $group, and $project, to process and aggregate data.
  13. What are replica sets in MongoDB? How do they ensure high availability?

    • Replica sets are groups of MongoDB servers that maintain the same data set. They ensure high availability by automatically failing over to a secondary server if the primary server goes down.
  14. How does MongoDB handle transactions?

    • MongoDB supports multi-document transactions, allowing multiple operations to be executed atomically. Transactions are started with session.startTransaction() and committed with session.commitTransaction().
  15. What is the difference between embedded and referenced documents?

    • Embedded documents store related data within a single document, while referenced documents store related data in separate documents and reference them using ObjectIds.
  16. How do you optimize queries in MongoDB?

    • Use indexes, avoid large documents, use projection to return only necessary fields, and analyze query performance with the explain() method.
  17. What is a capped collection? When would you use it?

    • A capped collection is a fixed-size collection that automatically overwrites the oldest documents when it reaches its size limit. It is useful for logging and caching scenarios.
  18. Explain the $lookup operator in MongoDB. How does it work?

    • The $lookup operator performs a left outer join to another collection in the same database. It allows you to combine data from multiple collections in a single query.
  19. How does MongoDB handle schema validation?

    • MongoDB supports JSON Schema validation, allowing you to enforce a schema on a collection using the validator option in db.createCollection().
  20. What is a covered query in MongoDB?

    • A covered query is a query where all the fields in the query and the returned results are part of an index. It improves performance by avoiding the need to read documents from disk.
  21. Describe MongoDB’s internal storage architecture.

    • MongoDB uses a storage engine (e.g., WiredTiger) to manage data storage. Data is stored in collections, which are composed of documents. Indexes are used to improve query performance.
  22. How do you manage large datasets in MongoDB for performance?

    • Use sharding, optimize indexes, use appropriate data models, and perform regular maintenance tasks like compacting and reindexing.
  23. What are the differences between MongoDB and other NoSQL databases like Cassandra or CouchDB?

    • MongoDB uses a document-oriented model, while Cassandra uses a wide-column model and CouchDB uses a document model with a focus on replication and synchronization. MongoDB supports rich queries and indexing, while Cassandra excels in write-heavy workloads and CouchDB focuses on offline-first applications.
  24. Explain the write concern and read concern levels in MongoDB.

    • Write concern specifies the level of acknowledgment requested from MongoDB for write operations. Read concern specifies the consistency and isolation properties of the data read from the database.
  25. How does MongoDB ensure data consistency in distributed systems?

    • MongoDB uses replica sets to ensure data consistency. Write operations are replicated to secondary members, and read operations can be configured to read from the primary or secondary members.
  26. What are MongoDB change streams, and how do they work?

    • Change streams allow applications to access real-time data changes in a collection. They use the watch() method to listen for changes and provide a stream of change events.
  27. Describe the WiredTiger storage engine. How does it differ from MMAPv1?

    • WiredTiger is the default storage engine in MongoDB, offering better compression, concurrency, and performance compared to the older MMAPv1 storage engine. WiredTiger uses a B-tree and LSM tree structure, while MMAPv1 uses memory-mapped files.
  28. How would you secure a MongoDB deployment in production?

    • Enable authentication, use role-based access control, encrypt data at rest and in transit, configure firewalls, and regularly update MongoDB to the latest version.
  29. What is the difference between findAndModify() and update()? When would you use one over the other?

    • findAndModify() atomically modifies and returns a single document, while update() modifies documents without returning them. Use findAndModify() when you need the modified document, and update() for bulk updates.
  30. Explain how MongoDB handles concurrency.

    • MongoDB uses a combination of optimistic and pessimistic concurrency control. It employs document-level locking and supports multi-document transactions to ensure data consistency and isolation.

Back to Top⤴️

Contributing

If you find any issues or have suggestions for improvement, feel free to contribute. Follow the contribution guidelines for details.

Back to Top⤴️

Acknowledgements

Back to Top⤴️

Conclusion

MongoDB is a powerful NoSQL database that offers flexibility, scalability, and performance for modern applications. By understanding the basic concepts, data modeling, indexing, and CRUD operations, you can leverage MongoDB effectively in your projects.

Back to Top⤴️

References

Back to Top⤴️

License

This repository is licensed under the MIT License.

Back to Top⤴️

Connect with me

Back to Top⤴️

Support

If you like this learning repository and find it useful, consider buying me a coffee or sponsoring me through the GitHub Sponsor. Your support will help me to continue and bring more exciting projects. Thank you!

Buy Me A Coffee

Sponsor Me


Show your support by 🌟 the repository.