DEV Community

Cover image for Essential Concepts | MongoDB | Part 1
Aakash Kumar
Aakash Kumar

Posted on

Essential Concepts | MongoDB | Part 1

Basics

SQL vs NoSQL,Documents and Collections, Data Types

SQL vs NoSQL

  1. SQL (Structured Query Language): Traditional relational databases like MySQL, PostgreSQL. They use tables to store data, and data is structured in rows and columns.Example: A table Users with columns id, name, email.

  2. NoSQL (Not Only SQL): More flexible data models like document databases (MongoDB), key-value stores, wide-column stores, etc.Example: A collection Users where each user is a JSON-like document.

Documents and Collections

Document: A record in a NoSQL database, typically stored in a JSON-like format.

Example:

{
  "id": 1,
  "name": "John Doe",
  "email": "john.doe@example.com"
}
Enter fullscreen mode Exit fullscreen mode

Collection: A group of documents, similar to a table in SQL.

Example: A collection Users containing documents like the one above.

Data Types

String: "John Doe"
Number: 25
Boolean: true
Array: ["reading", "traveling"]
Object: {"street": "123 Main St", "city": "Anytown"}

Methods

insert()
Example:
Insert a new user into the Users

collection.db.Users.insert({ name: "Alice", email: "alice@example.com" });
Enter fullscreen mode Exit fullscreen mode

find()
Example: Find all users in the Users

collection.db.Users.find();
Enter fullscreen mode Exit fullscreen mode

update()
Example: Update the email of a user with name "Alice".

db.Users.update({ name: "Alice" }, { $set: { email: "newalice@example.com" } });
Enter fullscreen mode Exit fullscreen mode

deleteOne()
Example: Delete a user with name "Alice".

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

bulkWrite()
Example: Perform multiple operations in a single call.

db.Users.bulkWrite([
  { insertOne: { document: { name: "Bob", email: "bob@example.com" } } },
  { updateOne: { filter: { name: "John Doe" }, update: { $set: { email: "newjohn@example.com" } } } },
  { deleteOne: { filter: { name: "Alice" } } }
]);
Enter fullscreen mode Exit fullscreen mode

Comparison Operators

$eq (Equal To)
Example: Find users with name "John Doe".

db.Users.find({ name: { $eq: "John Doe" } });
Enter fullscreen mode Exit fullscreen mode

$gt (Greater Than)
Example: Find users older than 25.

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

$lt (Less Than)
Example: Find users younger than 25.

db.Users.find({ age: { $lt: 25 } });
Enter fullscreen mode Exit fullscreen mode

$lte (Less Than or Equal To)
Example: Find users aged 25 or younger.

db.Users.find({ age: { $lte: 25 } });
Enter fullscreen mode Exit fullscreen mode

$gte (Greater Than or Equal To)
Example: Find users aged 25 or older.

db.Users.find({ age: { $gte: 25 } });
Enter fullscreen mode Exit fullscreen mode

$ne (Not Equal To)
Example: Find users not named "John Doe".

db.Users.find({ name: { $ne: "John Doe" } });
Enter fullscreen mode Exit fullscreen mode

Logical Operators

$and
Example: Find users named "John Doe" who are older than 25.

db.Users.find({ $and: [ { name: "John Doe" }, { age: { $gt: 25 } } ] });
Enter fullscreen mode Exit fullscreen mode

$or
Example: Find users named "John Doe" or younger than 25.

db.Users.find({ $or: [ { name: "John Doe" }, { age: { $lt: 25 } } ] });
Enter fullscreen mode Exit fullscreen mode

$not
Example: Find users not named "John Doe".

db.Users.find({ name: { $not: { $eq: "John Doe" } } });
Enter fullscreen mode Exit fullscreen mode

$nor
Example: Find users neither named "John Doe" nor older than 25.

db.Users.find({ $nor: [ { name: "John Doe" }, { age: { $gt: 25 } } ] });
Enter fullscreen mode Exit fullscreen mode

Array Operators

$in
Example: Find users whose names are either "John Doe" or "Alice".

db.Users.find({ name: { $in: ["John Doe", "Alice"] } });
Enter fullscreen mode Exit fullscreen mode

$nin
Example: Find users whose names are neither "John Doe" nor "Alice".

db.Users.find({ name: { $nin: ["John Doe", "Alice"] } });
Enter fullscreen mode Exit fullscreen mode

$all
Example: Find users who have both "reading" and "traveling" in their hobbies.

db.Users.find({ hobbies: { $all: ["reading", "traveling"] } });
Enter fullscreen mode Exit fullscreen mode

$elemMatch
Example: Find users who have an address in "New York".

db.Users.find({ addresses: { $elemMatch: { city: "New York" } } });
Enter fullscreen mode Exit fullscreen mode

$size
Example: Find users who have exactly 2 hobbies.

db.Users.find({ hobbies: { $size: 2 } });
Enter fullscreen mode Exit fullscreen mode

Element Operators

$exists
Example: Find users who have an email address.

db.Users.find({ email: { $exists: true } });
Enter fullscreen mode Exit fullscreen mode

$type
Example: Find users whose age is a number.

db.Users.find({ age: { $type: "number" } });
Enter fullscreen mode Exit fullscreen mode

$regex
Example: Find users whose email ends with "example.com".

db.Users.find({ email: { $regex: /example\.com$/ } });
Enter fullscreen mode Exit fullscreen mode

Projection Operators

$project
Example: Include only the name and email fields.

db.Users.find({}, { name: 1, email: 1 });
Enter fullscreen mode Exit fullscreen mode

$include and $exclude
Example: Exclude the age field.

db.Users.find({}, { age: 0 });
Enter fullscreen mode Exit fullscreen mode

$slice
Example: Limit the array to the first 3 elements.

db.Users.find({}, { hobbies: { $slice: 3 } });
Enter fullscreen mode Exit fullscreen mode

Indexes

Single Field
Example: Create an index on the email field.

db.Users.createIndex({ email: 1 });
Enter fullscreen mode Exit fullscreen mode

Compound
Example: Create a compound index on name and email.

db.Users.createIndex({ name: 1, email: 1 });
Enter fullscreen mode Exit fullscreen mode

Text
Example: Create a text index on the description field.

db.Users.createIndex({ description: "text" });
Enter fullscreen mode Exit fullscreen mode

These concepts and examples provide a comprehensive overview of MongoDB operations, queries, aggregation, transactions, and security measures. If you need more details or have specific scenarios to explore, feel free to ask!

Happy Coding 🧑‍💻

Connect with Me 🙋🏻: LinkedIn

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more