Basics
SQL vs NoSQL,Documents and Collections, Data Types
SQL vs NoSQL
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.
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"
}
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" });
find()
Example: Find all users in the Users
collection.db.Users.find();
update()
Example: Update the email of a user with name "Alice".
db.Users.update({ name: "Alice" }, { $set: { email: "newalice@example.com" } });
deleteOne()
Example: Delete a user with name "Alice".
db.Users.deleteOne({ name: "Alice" });
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" } } }
]);
Comparison Operators
$eq (Equal To)
Example: Find users with name "John Doe".
db.Users.find({ name: { $eq: "John Doe" } });
$gt (Greater Than)
Example: Find users older than 25.
db.Users.find({ age: { $gt: 25 } });
$lt (Less Than)
Example: Find users younger than 25.
db.Users.find({ age: { $lt: 25 } });
$lte (Less Than or Equal To)
Example: Find users aged 25 or younger.
db.Users.find({ age: { $lte: 25 } });
$gte (Greater Than or Equal To)
Example: Find users aged 25 or older.
db.Users.find({ age: { $gte: 25 } });
$ne (Not Equal To)
Example: Find users not named "John Doe".
db.Users.find({ name: { $ne: "John Doe" } });
Logical Operators
$and
Example: Find users named "John Doe" who are older than 25.
db.Users.find({ $and: [ { name: "John Doe" }, { age: { $gt: 25 } } ] });
$or
Example: Find users named "John Doe" or younger than 25.
db.Users.find({ $or: [ { name: "John Doe" }, { age: { $lt: 25 } } ] });
$not
Example: Find users not named "John Doe".
db.Users.find({ name: { $not: { $eq: "John Doe" } } });
$nor
Example: Find users neither named "John Doe" nor older than 25.
db.Users.find({ $nor: [ { name: "John Doe" }, { age: { $gt: 25 } } ] });
Array Operators
$in
Example: Find users whose names are either "John Doe" or "Alice".
db.Users.find({ name: { $in: ["John Doe", "Alice"] } });
$nin
Example: Find users whose names are neither "John Doe" nor "Alice".
db.Users.find({ name: { $nin: ["John Doe", "Alice"] } });
$all
Example: Find users who have both "reading" and "traveling" in their hobbies.
db.Users.find({ hobbies: { $all: ["reading", "traveling"] } });
$elemMatch
Example: Find users who have an address in "New York".
db.Users.find({ addresses: { $elemMatch: { city: "New York" } } });
$size
Example: Find users who have exactly 2 hobbies.
db.Users.find({ hobbies: { $size: 2 } });
Element Operators
$exists
Example: Find users who have an email address.
db.Users.find({ email: { $exists: true } });
$type
Example: Find users whose age is a number.
db.Users.find({ age: { $type: "number" } });
$regex
Example: Find users whose email ends with "example.com".
db.Users.find({ email: { $regex: /example\.com$/ } });
Projection Operators
$project
Example: Include only the name and email fields.
db.Users.find({}, { name: 1, email: 1 });
$include and $exclude
Example: Exclude the age field.
db.Users.find({}, { age: 0 });
$slice
Example: Limit the array to the first 3 elements.
db.Users.find({}, { hobbies: { $slice: 3 } });
Indexes
Single Field
Example: Create an index on the email field.
db.Users.createIndex({ email: 1 });
Compound
Example: Create a compound index on name and email.
db.Users.createIndex({ name: 1, email: 1 });
Text
Example: Create a text index on the description field.
db.Users.createIndex({ description: "text" });
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
Top comments (0)