Introduction to Mongo Database

Madhav Ganesan - Oct 29 - - Dev Community

MongoDB is a popular NoSQL database that uses a document-oriented data model. Instead of storing data in tables as in relational databases, MongoDB stores data in JSON-like documents, which allows for more flexible and hierarchical data structures.

Mongodb default port number

27017

BSON (Binary JSON)

It is a binary representation of JSON-like documents. This allows for more efficient storage and retrieval compared to text-based formats like JSON. It is designed for efficient encoding and decoding. It enhances MongoDB's performance, flexibility, and functionality by extending the JSON format to better suit the needs of a NoSQL database.

Replica Sets

A replica set in MongoDB is a group of MongoDB servers (or instances) that host the same database to provide redundancy and high availability

Primary Node: This server is responsible for handling all write operations and may also serve read requests.
Secondary Nodes: These servers replicate the data from the primary and can handle read requests (depending on the configuration)

If the primary node fails, the replica set automatically elects a new primary from the secondary nodes, ensuring that write operations can continue with minimal disruption.

All nodes in a replica set contain copies of the same data. Changes made to the primary are asynchronously replicated to the secondary nodes.

// Connect to the primary node
mongo --port 27017

// In the mongo shell, initialize the replica set
rs.initiate({
  _id: "myReplicaSet",
  members: [
    { _id: 0, host: "localhost:27017" },
    { _id: 1, host: "localhost:27018" },
    { _id: 2, host: "localhost:27019" }
  ]
});
Enter fullscreen mode Exit fullscreen mode

Sharding

It is a method used to distribute data across multiple servers, or shards, to improve performance and scalability. This is particularly useful when dealing with large datasets and high-throughput applications. It allows MongoDB to scale horizontally by adding more servers.

It divides data into chunks and distributes these chunks across multiple shards based on a shard key. Each shard holds a subset of the data.

Types of Scaling

Horizontal Scaling
This involves adding more servers or instances to handle increased load or data, distributing the workload across multiple nodes.

Vertical Scaling
This involves increasing the resources (CPU, RAM, storage) of a single server to handle more load or data.

Aggregation Pipeline

MongoDB Aggregation Pipelines are a powerful feature that allows you to process and analyze data in MongoDB through a series of stages. Each stage performs a specific operation on the data, and the results are passed to the next stage in the pipeline. This allows for complex data transformations, filtering, and computations.

Example:
Calculate the total sales amount for each product.

// Connect to the MongoDB instance
const { MongoClient } = require('mongodb');

async function run() {
  const client = new MongoClient('mongodb://localhost:27017');
  try {
    await client.connect();
    const db = client.db('store');
    const collection = db.collection('sales');

    // Define the aggregation pipeline
    const pipeline = [
      {
        $group: {
          _id: "$product",
          totalSales: { $sum: { $multiply: ["$quantity", "$price"] } }
        }
      },
      {
        $project: {
          _id: 0,
          product: "$_id",
          totalSales: 1
        }
      }
    ];

    // Execute the aggregation pipeline
    const results = await collection.aggregate(pipeline).toArray();
    console.log(results);
  } finally {
    await client.close();
  }
}

run().catch(console.dir);
Enter fullscreen mode Exit fullscreen mode

Output:

[
  { "product": "Laptop", "totalSales": 2000 },
  { "product": "Smartphone", "totalSales": 2500 },
  { "product": "Tablet", "totalSales": 900 }
]
Enter fullscreen mode Exit fullscreen mode

Capped Collection

It is a fixed-size collection that automatically overwrites the oldest documents when it reaches its size limit. It maintains the insertion order of documents.

 db.createCollection(CollectionName, {capped:true, size : 100000})
Enter fullscreen mode Exit fullscreen mode

Insert

db.collection.insertOne({ key: "value" });
db.collection.insertMany([])
Enter fullscreen mode Exit fullscreen mode

Find

db.collection.find({ key: "value" });
db.collection.find({ age: { $gte: 20 } })
Enter fullscreen mode Exit fullscreen mode

Limit

db.collection.find({ status: "active" }).limit(5) // Limits the result to 5 documents
Enter fullscreen mode Exit fullscreen mode

Update

db.collection.updateOne({ key: "value" }, { $set: { key: "new_value" } });

db.collection.updateOne(
  { _id: 1 },
  { $set: { name: "New Name", age: 25 } }
)
Enter fullscreen mode Exit fullscreen mode

Upsert

"upsert" is a combination of "update" and "insert." It means that if the document matching the update criteria does not exist, a new document will be created with the specified fields.

db.collection.updateOne(
  { _id: 1 },
  { $set: { name: "Upsert Example" } },
  { upsert: true }
)
Enter fullscreen mode Exit fullscreen mode

Delete

db.collection.deleteOne()
db.collection.deleteMany().
Enter fullscreen mode Exit fullscreen mode

Index

db.collection.createIndex({ "name": 1 })
{ "_id": 1, "name": "Alice" }
{ "_id": 2, "name": "Bob" }
{ "_id": 3, "name": "Charlie" }
{ "_id": 4, "name": "Alice" }
Enter fullscreen mode Exit fullscreen mode

After creating an ascending index on the name field:

Index Structure: The index will organize the name values in alphabetical order:

Alice (IDs 1 and 4)
Bob (ID 2)
Charlie (ID 3)
Query Efficiency: If you query for documents where the name is "Alice", MongoDB can quickly find the entries for Alice by looking up the index, which points directly to the documents with this name.

db.collection.createIndex()
db.collection.dropIndex()
db.collection.dropIndexes()
Enter fullscreen mode Exit fullscreen mode

Compound Index
This type of index is used to index multiple fields within a single index.

db.collection.createIndex({ "field1": 1, "field2": -1 })
Enter fullscreen mode Exit fullscreen mode

Collection

A collection in MongoDB is a grouping of documents
A collection is a group of MongoDB documents, similar to a table in a relational database. Collections do not enforce a schema, which means documents within a collection can have different fields and data types.

db.createCollection()
db.dropCollection()
db.dropDatabase()
Enter fullscreen mode Exit fullscreen mode

Populate

In Mongoose, populate is used to automatically replace the specified paths in the document with documents from other collections.
This is useful for handling relationships between documents, such as when one document references another.
way to "display" or enrich your data with additional related content from other collections

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Define the Author schema
const authorSchema = new Schema({
  name: String,
  birthYear: Number
});

// Define the Book schema
const bookSchema = new Schema({
  title: String,
  authorId: { type: Schema.Types.ObjectId, ref: 'Author' },
  publicationYear: Number
});

// Create the models
const Author = mongoose.model('Author', authorSchema);
const Book = mongoose.model('Book', bookSchema);


// Create a new author
const author = new Author({ name: 'J.K. Rowling', birthYear: 1965 });
author.save();

// Create a new book with a reference to the author
const book = new Book({
  title: 'Harry Potter and the Sorcerer\'s Stone',
  authorId: author._id,
  publicationYear: 1997
});
book.save();


// Find a book and populate the authorId field with the author's document
Book.findOne({ title: 'Harry Potter and the Sorcerer\'s Stone' })
  .populate('authorId') // Populate the authorId field
  .exec((err, book) => {
    if (err) throw err;
    console.log(book);
  });
Enter fullscreen mode Exit fullscreen mode

Document

Document is the basic unit of data. Documents are stored in collections and are analogous to rows in a relational database

Data types:

String: It is used to store text. Strings must be UTF-8 encoded.
Integer: It is used to store numeric values. MongoDB supports both 32-bit and 64-bit integers
Boolean: It is used to store Boolean values, which can be either true or false.
Double: It is used to store floating-point values
Arrays: It is used to store lists or multiple values in a single field
Objects (Documents): It is used to store embedded documents
Date: It is used to store date and time values(milliseconds)
Null: It is used to represent the absence of a value or a null value.
Binary Data: Binary data
Regular Expression: Regular expression
JavaScript Code: JavaScript code
Timestamp: Timestamp

Stay Connected!
If you enjoyed this post, don’t forget to follow me on social media for more updates and insights:

Twitter: madhavganesan
Instagram: madhavganesan
LinkedIn: madhavganesan

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .