Careeroza
Operations
Basic Operaitons
Query Operators in MongoDB
Query operators are used to filter data.
Comparison Operators
$gt → greater than
$lt → less than
$gte → greater than or equal
$lte → less than or equal
$ne → not equalExample:
db.users.find({ age: { $gt: 25 } })Logical Operators
-
$and → all conditions must match
-
$or → any condition matches
-
$not → reverse condition
$and → all conditions must match
$or → any condition matches
$not → reverse condition
Example:
db.users.find({ $or: [ { age: 25 }, { age: 30 } ] })Element Operators
-
$exists → checks if field exists
-
$type → checks data type
$exists → checks if field exists
$type → checks data type
🔹 Sort, Limit, Skip
Sort
Used to order results
db.users.find().sort({ age: 1 })
1 → ascending
-1 → descendingLimit
Restrict number of results
db.users.find().limit(5)
Restrict number of results
db.users.find().limit(5)Skip
Skip some documents
db.users.find().skip(5)Used for pagination (like page 2, page 3)
Dot Notation
Used to access nested fields (data inside objects).
Example document:
name: "Rahul"
address: { city: "Hyderabad", pincode: 500001 }
Query:
db.users.find({ "address.city": "Hyderabad" })
“address.city” is dot notationBSON Data Types
MongoDB stores data in BSON (Binary JSON).
Common types:
- String → "Rahul"
- Number → 25
- Boolean → true/false
- Array → [1, 2, 3]
- Object → { city: "Hyd" }
- Date → ISODate
- Null
BSON supports more types than JSON
ObjectId is a special BSON type used for _id.
Automatically created for each document
Example:
_id: ObjectId("661f8c9a2f1b2c3d4e5f6789")
What ObjectId contains
-
Timestamp (when created)
-
Machine ID
-
Process ID
-
Counter
That’s why it is unique
Simple Understanding
-
Query operators → filter data
-
Sort → order data
-
Limit → restrict results
-
Skip → pagination
-
Dot notation → access nested fields
-
BSON → data format
-
ObjectId → unique identifier
1. Insert Multiple Documents
db.users.insertMany([
{ name: "Rahul", age: 25 },
{ name: "Anu", age: 22 }
])Used when you want to add many records at once
2. Find with Conditions
db.users.find({ age: { $gt: 23 } })$gt means “greater than”
Returns users whose age is more than 23
Other operators:
-
$lt→ less than -
$gte→ greater than or equal -
$lte→ less than or equal
3. Find Specific Fields
db.users.find({}, { name: 1, age: 1 })
Shows only name and age
1 means show field, 0 means hide
4. Update Multiple Documents
db.users.updateMany(
{ age: { $lt: 25 } },
{ $set: { status: "young" } }
)Adds/updates field status for many users
🔹 5. Remove a Field
db.users.updateOne(
{ name: "Rahul" },
{ $unset: { age: "" } }
)Deletes the age field from document
6. Count Documents
db.users.countDocuments()
Returns total number of documents
With condition:
db.users.countDocuments({ age: 25 })🔹 7. Sort Data
db.users.find().sort({ age: 1 })
1 → ascending
-1 → descending🔹 8. Limit Results
db.users.find().limit(2)
Returns only first 2 documents🔹 9. Skip Documents
db.users.find().skip(2)
Skips first 2 documents
Used in pagination (like page 2, page 3)🔹 10. Drop Database
db.dropDatabase()
Deletes entire database ⚠️ (use carefully)