_id
field is a unique identifier automatically generated for each document{
"_id": 1,
"name": “Pranav",
"address": {
"street": "123 Elm St",
"city": "Somewhere",
}
}
{
"_id": 1,
"name": "Amit Sharma",
"age": 16,
"address": {
"street": "MG Road",
"city": "Ahmedabad",
"pincode": "380015"
}
}
_id
) is used to link them{
"_id": 1,
"name": "Amit Sharma",
"age": 16,
"address_id": 101
}
address_id
in the student's document references the _id
of the address document in the Addresses collection. To get the complete information about a student, you would need to join or query both collections.Aspect | Embedding | Referencing |
---|---|---|
Srtucture | Nested data within the same document | Related data stored separately |
Performance | Faster reads, fewer queries | Slower reads due to joins |
Data Duplication | May lead to duplication | Reduces duplication |
Flexibility | Harder to u date shared data | Easier to update shared data |
Use Case | Simple, closely related data | Complex, reusable data |
db.collection.find({query})
$eq
: Matches values that are equal to a specified value.db.emp.find({ deptname: { $eq: "IT" } })
$ne
: Matches values that are not equal.db.emp.find({ deptname: { $ne: "IT" } })
$gt
: Matches values greater than a specified value.db.emp.find({ salary: { $gt: 80000 } })
$l
t: Matches values less than a specified value.db.emp.find({ salary: { $lt: 80000 } })
$gte
: Matches values greater than or equal to.db.emp.find({ empid: { $gte: 22 } })
$lte
: Matches values less than or equal todb.emp.find({ empid: { $lte: 5 } })
$in
: Allows you to match documents where a field’s value is within a specified arraydb.emp.find({ empid: { $in: [1,3,5,8] } })
$nin
: Selects those documents where the field value is not equal to any of the given values in the array and the field that does not exist.db.emp.find({ empid: { $nin: [1,3,5,8] } })
$and
: Joins query clauses with a logical ANDdb.emp.find({ $and: [ { deptname: "Sales" }, { salary: { $gt: 40000 } } ] })
$or
: Joins query clauses with a logical ORdb.emp.find({ $or: [ { deptname: "HR" }, { salary: { $lt: 30000 } } ] }
$not
: Inverts the effect of a query expression.db.emp.find({ city: { $not: { $regex: "^B" } } })
$nor
: Joins query clauses with a logical NOR.db.emp.find({$nor: [{salary: 3000}, {deptname: "IT"}]})
distinct
db.emp.distinct(“deptname")
$exists
: Matches documents that have the specified field.db.emp.find({ gender: { $exists: true } })
$type
: Selects documents if a field is of the specified type.db.emp.find({ salary: { $type: "number" } })
db.students.find({}, { name: 1, age: 1 })
db.students.find({}, { address: 0 })
_id
field. To exclude it:db.students.find({}, { _id: 0, name: 1, age: 1 })
insertOne()
or insertMany()
methods.// Switch to a database
use school
// Insert a single document
db.students.insertOne({
name: "John Doe",
age: 21,
department: "Computer Science"
})
// Insert multiple documents
db.students.insertMany([
{ name: "Jane Doe", age: 22, department: "Mathematics" },
{ name: "Sam Smith", age: 20, department: "Physics" }
])
find()
method.// Find all documents in the students collection
db.students.find()
// Find a document with a specific condition
db.students.find({ age: 21 })
// Find documents and format the output
db.students.find({}, { name: 1, _id: 0 })
updateOne()
, updateMany()
methods.// Update a single document
db.students.updateOne(
{ name: "John Doe" },
{ $set: { age: 22 } }
)
// Update multiple documents
db.students.updateMany(
{ department: "Physics" },
{ $set: { department: "Astrophysics" } }
)
deleteOne()
or deleteMany()
methods// Delete a single document
db.students.deleteOne({ name: "Sam Smith" })
// Delete multiple documents
db.students.deleteMany({ age: { $lt: 21 } })
$match
: Filters the documents to pass only those that match the specified condition.
$group
: Groups input documents by a specified identifier and applies accumulator expressions.
$project
: Shapes the documents by including or excluding fields.
$sort
: Sorts the documents in ascending or descending order.
$limit
: Limits the number of documents passed to the next stage.
$skip
: Skips over a specified number of documents.
Let's consider a sales collection that contains documents about sales transactions.
use store
db.sales.insertMany([
{ item: "apple", quantity: 10, price: 5, date: new Date("2023-01-01")},
{ item: "banana", quantity: 5, price: 2, date: new Date("2023-01-02")},
{ item: "orange", quantity: 8, price: 3, date: new Date("2023-01-03")},
{ item: "apple", quantity: 3, price: 5, date: new Date("2023-01-04")},
{ item: "banana", quantity: 7, price: 2, date: new Date("2023-01-05")}
])
$match
:// Filter sales of apples
db.sales.aggregate([
{ $match: { item: "apple" } }
])
$group
:// Group sales by item and calculate the total quantity sold:
db.sales.aggregate([
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" } } }
])
$project
:// Project only the item and total cost (quantity * price):
db.sales.aggregate([
{ $project: { item: 1, totalCost: { $multiply: ["$quantity", "$price"]}, _id: 0 } }
])
$sort
:// Sort items by total quantity in descending order:
db.sales.aggregate([
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" } } },
{ $sort: { totalQuantity: -1 } }
])
$limit
:// Limit the result to the top 2 items with the highest total quantity:
db.sales.aggregate([
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" } } },
{ $sort: { totalQuantity: -1 } },
{ $limit: 2 }
])
$skip
:// Skip the first item and display the next one:
db.sales.aggregate([
{ $group: { _id: "$item", totalQuantity: { $sum: "$quantity" } } },
{ $sort: { totalQuantity: -1 } },
{ $skip: 1 },
{ $limit: 1 }
])
Made By SOU Student for SOU Students