DEV Community

Cover image for Day 32 of #100DaysOfCode — MongoDB Basics
M Saad Ahmad
M Saad Ahmad

Posted on

Day 32 of #100DaysOfCode — MongoDB Basics

MongoDB is a popular NoSQL database designed for high-performance, flexible, and scalable data storage. It stores data in JSON-like documents, allowing dynamic and nested structures that fit modern web applications. With its ease of use and powerful querying capabilities, MongoDB has become a go-to database for developers.

Day 32 was all about understanding MongoDB basics, CRUD operations, operators, and how to start coding with it.


What is MongoDB?

MongoDB is a NoSQL database, designed to store, manage, and retrieve large volumes of data efficiently. Unlike traditional relational databases (SQL), MongoDB is document-oriented, meaning it stores data in flexible JSON-like documents.

This makes it ideal for applications that require scalability, flexibility, and high performance, such as web apps, real-time analytics, and content management systems.


How MongoDB Stores and Organizes Data

MongoDB organizes data using these core structures:

  • Database: The highest-level container.
  • Collection: Equivalent to a table in SQL, a collection stores multiple documents.
  • Document: The smallest unit, similar to a row in SQL, but stored as a BSON (Binary JSON) object.

Each document can have nested fields and arrays, allowing dynamic schema design. For example:

{
  "name": "Alice",
  "age": 25,
  "skills": ["JavaScript", "MongoDB", "Node.js"],
  "address": {
    "city": "New York",
    "zip": "10001"
  }
}
Enter fullscreen mode Exit fullscreen mode

Two Ways to Work with MongoDB

You can start coding with MongoDB in two main ways:

1. Using MongoDB Atlas (Cloud)

MongoDB Atlas is a cloud-hosted MongoDB service that allows you to create and manage databases without local installation.

  • Create an account at MongoDB Atlas
  • Create a free cluster in a few clicks
  • Connect using a connection string in your application

2. Installing MongoDB Locally

You can also install MongoDB on your system for local development.

  • Follow the installation guide here: Install MongoDB Community Server
  • Once installed, you can run MongoDB on your local machine and connect via Mongo Shell or a GUI like MongoDB Compass

Basic CRUD Operations in MongoDB

Here’s how to perform the CRUD operations using MongoDB:

1. Insert a Document

db.users.insertOne({
  name: "Alice",
  age: 25,
  city: "New York"
});
Enter fullscreen mode Exit fullscreen mode

2. Read Documents

db.users.find({ age: { $gt: 20 } });
Enter fullscreen mode Exit fullscreen mode

3. Update Documents

db.users.updateOne(
  { name: "Alice" },
  { $set: { age: 26 } }
);
Enter fullscreen mode Exit fullscreen mode

4. Delete Documents

db.users.deleteOne({ name: "Alice" });
Enter fullscreen mode Exit fullscreen mode

Important MongoDB Operators

MongoDB provides powerful operators to query and manipulate data:

  • $eq – Equals
  • $gt – Greater than
  • $lt – Less than
  • $in – Matches any value in an array
  • $set – Update a field
  • $unset – Remove a field
  • $push – Add to an array

Basic BSON Types in MongoDB

MongoDB stores data in BSON (Binary JSON) format, supporting the following common types:

Type Description
String Text data
Number (Int, Double, Decimal) Numeric data
Boolean True/False
Date Timestamp
Array List of values
ObjectId Unique identifier for documents

Conclusion

MongoDB is a flexible, scalable, and powerful NoSQL database perfect for modern web and mobile applications. By understanding its structure, operators, BSON types, and CRUD operations, you’re ready to start building real-world projects.

Whether you choose MongoDB Atlas for cloud convenience or local installation for hands-on practice, MongoDB is a fantastic skill to add to your developer toolkit.

Thanks for reading. Feel free to share your thoughts!

Top comments (0)