Open true-matrix opened 7 months ago
"_id" : ObjectId("65d5964c1172d5d9824698fd"),
"StudentNo" : "1",
"FirstName" : "Max",
"LastName" : "Gole",
"Age" : "10"
}
{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"StudentNo" : "2",
"FirstName" : "Tax",
"LastName" : "Koe",
"Age" : "9"
}
{
"_id" : ObjectId("65d597481172d5d9824698ff"),
"StudentNo" : "3",
"FirstName" : "Fax",
"LastName" : "Gole",
"Age" : "10"
}
11. Query Document - AND
db.students.find({ "FirstName" : "Tax", "Age" :"9" })
12. Query Document - OR
db.students.find({ $or : [{"FirstName" : "Max"},{"Age" : "9"}] })
13. Query Document -AND OR
db.students.find({ "FirstName" : "Max", $or : [{"Age" : "10"},{"Age" : "9"}] })
14. Update Single Document
db.students.update( {"_id" : ObjectId("65d597481172d5d9824698ff")}, {$set : {"Age" : "12"}} )
⏺update the document by inserting a new nested field like: Address and inside address there is city and pincode
db.students.updateOne(
{ "StudentNo": "1" },
{
$set: {
"Address.City": "Mohali",
"Address.Pincode": "12345"
}
}
);
⏺Insert nested array of objects
db.students.updateOne(
{ "StudentNo": "2" },
{
$set: {
"Address": [
{
"City": "Salt Lake - 1",
"Pincode": "700092"
},
{
"City": "Salt Lake - 2",
"Pincode": "700093"
},
]
}
}
);
15. Update Many fields in Document
db.students.update( {"Age" : "10"}, {$set : {"LastName" : "Gole"}}, {multi : true} )
16. Remove all Document
db.students.remove()
17. Remove all Document
db.students.remove()
18. Remove a specific Document
db.students.remove({ "_id" : ObjectId("65d5964c1172d5d9824698fd"), })
19. Remove one Document of Age=16 (althouth multiple document matches this conditions)
db.students.remove({ "Age" : "16", },1)
✅MongoDB - Projection -> projection means selecting only necessary data rather than selecting whole of the data of a document
db.COLLECTION_NAME.find({},{KEY:1})`
**20. Input : `db.students.find({},{"FirstName" : 1})`**
Output : `{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"FirstName" : "Tax"
}
{
"_id" : ObjectId("65d597481172d5d9824698ff"),
"FirstName" : "Fax"
}
21. Input : db.students.find({},{"FirstName" : 1, "_id":0})
Output : { "FirstName" : "Tax" } { "FirstName" : "Fax" }
22. limit
using limit how many data we want to show
Input : db.students.find({},{"StudentNo": 1, "FirstName" : 1, "_id" : 0}).limit(1)
Output : { "StudentNo" : "2", "FirstName" : "Tax" }
23. skip
*using skip how many data we want to skip from first
Input : db.students.find({},{"StudentNo": 1, "FirstName" : 1, "_id" : 0}).skip(1)
Output : { "StudentNo" : "3", "FirstName" : "Fax" }
24. skip
*using skip how many data we want to skip from first
Input : db.students.find({},{"StudentNo": 1, "FirstName" : 1, "_id" : 0}).skip(1)
Output : { "StudentNo" : "3", "FirstName" : "Fax" }
25. skip *using skip and limit together if we want to skip first 1 result and show 2 result after skip in this example
Input : db.students.find({},{"StudentNo": 1, "FirstName" : 1, "_id" : 0}).skip(1).limit(2)
Output : { "StudentNo" : "3", "FirstName" : "Fax" }, { "StudentNo" : "4", "FirstName" : "Rax" }
26. sort
Sort in ascending order use 1
, for descending order -1
Input : db.students.find({},{"StudentNo": 1, "FirstName" : 1, "_id" : 0}).sort({"FirstName" : 1})
Output : { "StudentNo" : "3", "FirstName" : "Fax" } { "StudentNo" : "2", "FirstName" : "Tax" }
🔵What are Indexes/Indexing in MongoDB?
Indexes are the special data structure that stores the small portion of data set in the easy to pass way
🟠Why use Indexes in MongoDB?
Indexes are data structures that support the efficient execution of queries in MongoDB. They contain copies of parts of the data in documents to make queries more efficient. Without indexes, MongoDB must scan every document in a collection to find the documents that match each query
🔵Distinct in MongoDB? Distinct is used to find the unique values
{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"StudentNo" : "2",
"FirstName" : "Tax",
"LastName" : "Koe",
"Age" : "9"
}
{
"_id" : ObjectId("65d597481172d5d9824698ff"),
"StudentNo" : "3",
"FirstName" : "Fax",
"LastName" : "Gole",
"Age" : "10"
}
{
"_id" : ObjectId("65d5bd5b768e0e2b126763ee"),
"StudentNo" : "1",
"FirstName" : "Lyca",
"LastName" : "Vela",
"Age" : "8"
}
{
"_id" : ObjectId("65d5bd5b768e0e2b126763ef"),
"StudentNo" : "4",
"FirstName" : "Aham",
"LastName" : "Bill",
"Age" : "10"
}
Input : db.students.distinct('Age')
Output : [ "10", "8", "9" ]
Input : db.students.distinct('Age', {"StudentNo" : {"$gte" : "2"}})
Output : [ "10", "9" ]
🟣Count()
db.students.find().count()
🔵elemMatch in MongoDB? To find the document by using the conditions which data is in nested array of objects
{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"StudentNo" : "2",
"FirstName" : "Tax",
"LastName" : "Koe",
"Age" : "9",
"Address" : [
{
"City" : "Salt Lake - 1",
"Pincode" : "700092"
},
{
"City" : "Salt Lake - 2",
"Pincode" : "700093"
}
]
}
✅db.students.find({"Address" : {$elemMatch: {"Pincode" : "700092"}}})
$match:
Filters the documents based on specified criteria.
$project:
Shapes the documents, selecting specific fields, adding new fields, or renaming existing ones.
$group:
Groups documents by a specified key and performs aggregation operations on the grouped data.
$sort:
Sorts the documents based on specified criteria.
$skip:
Skips a specified number of documents in the pipeline.
$limit:
Limits the number of documents passed to the next stage in the pipeline.
$unwind:
Deconstructs an array field, producing a separate document for each element in the array.
$lookup:
Performs a left outer join to another collection in the same database.
$addFields:
Adds new fields to documents.
$replaceRoot:
Replaces the input document with a specified document.
$facet
: Enables the execution of multiple pipelines within a single stage.
✅ $match
db.collection.aggregate([ { $match: { field1: value1, field2: value2 // Add more conditions as needed } }, // Additional stages in the pipeline ]);
{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"StudentNo" : "2",
"FirstName" : "Tax",
"LastName" : "Koe",
"Age" : "9",
"Address" : [
{
"City" : "Salt Lake - 1",
"Pincode" : "700092"
},
{
"City" : "Salt Lake - 2",
"Pincode" : "700093"
}
],
"Status" : "Active"
}
{
"_id" : ObjectId("65d597481172d5d9824698ff"),
"StudentNo" : "3",
"FirstName" : "Fax",
"LastName" : "Gole",
"Age" : "10",
"Status" : "Inactive"
}
{
"_id" : ObjectId("65d5bd5b768e0e2b126763ee"),
"StudentNo" : "1",
"FirstName" : "Lyca",
"LastName" : "Vela",
"Age" : "8",
"Address" : {
"City" : "Mohali",
"Pincode" : "12345"
},
"Status" : "Active"
}
{
"_id" : ObjectId("65d5bd5b768e0e2b126763ef"),
"StudentNo" : "4",
"FirstName" : "Aham",
"LastName" : "Bill",
"Age" : "10",
"Address" : {
"City" : "Mumbai",
"Pincode" : "400236"
},
"Status" : "Active"
}
👍 Input : db.students.aggregate([{$match : {'Status' : "Inactive"}}])
☑Output :
"_id" : ObjectId("65d597481172d5d9824698ff"),
"StudentNo" : "3",
"FirstName" : "Fax",
"LastName" : "Gole",
"Age" : "10",
"Status" : "Inactive"
}
✅ $group
{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"StudentNo" : "2",
"FirstName" : "Tax",
"LastName" : "Koe",
"Age" : "9",
"Address" : [
{
"City" : "Salt Lake - 1",
"Pincode" : "700092"
},
{
"City" : "Salt Lake - 2",
"Pincode" : "700093"
}
],
"Status" : "Active",
"Department" : "Science",
"Marks" : NumberInt(60)
}
{
"_id" : ObjectId("65d597481172d5d9824698ff"),
"StudentNo" : "3",
"FirstName" : "Fax",
"LastName" : "Gole",
"Age" : "10",
"Status" : "Inactive",
"Department" : "Commerce",
"Marks" : NumberInt(88)
}
{
"_id" : ObjectId("65d5bd5b768e0e2b126763ee"),
"StudentNo" : "1",
"FirstName" : "Lyca",
"LastName" : "Vela",
"Age" : "8",
"Address" : {
"City" : "Mohali",
"Pincode" : "12345"
},
"Status" : "Active",
"Department" : "Arts",
"Marks" : NumberInt(90)
}
{
"_id" : ObjectId("65d5bd5b768e0e2b126763ef"),
"StudentNo" : "4",
"FirstName" : "Aham",
"LastName" : "Bill",
"Age" : "10",
"Address" : {
"City" : "Mumbai",
"Pincode" : "400236"
},
"Status" : "Active",
"Department" : "Science",
"Marks" : NumberInt(67)
}
Input :
{
$group: {
_id: "$Department",
totMarks: { $sum: "$Marks" }
}
}
]);
Output :
{
"_id" : "Commerce",
"totMarks" : NumberInt(88)
}
{
"_id" : "Arts",
"totMarks" : NumberInt(90)
}
{
"_id" : "Science",
"totMarks" : NumberInt(127)
}
✅ $project
Input :
db.students.aggregate([{$match : {Status : "Active"}},{$project : {"StudentNo":1, "FirstName":1, "_id":0}}])
Output :
{
"StudentNo" : "2",
"FirstName" : "Tax"
}
{
"StudentNo" : "1",
"FirstName" : "Lyca"
}
{
"StudentNo" : "4",
"FirstName" : "Aham"
}
✅ $project and $type
Input :
db.students.aggregate([{$match : {Status : "Active"}},{$project : {"StudentNo":{$type:"$StudentNo"}, "Marks":{$type:"$Marks"}, "_id":{$type : "$_id"}}}])
Output :
{
"StudentNo" : "string",
"Marks" : "int",
"_id" : "objectId"
}
{
"StudentNo" : "string",
"Marks" : "int",
"_id" : "objectId"
}
{
"StudentNo" : "string",
"Marks" : "int",
"_id" : "objectId"
}
✅ $unwind unwind : we can seperate the array {$unwind : "$arrayFieldName"}
👉Input : (Without unwind)
db.students.aggregate([{$match : {StudentNo: "2"}}, {$project: {"Address":1, "FirstName":1}}])
👉Output : (Without unwind)
{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"FirstName" : "Tax",
"Address" : [
{
"City" : "Salt Lake - 1",
"Pincode" : "700092"
},
{
"City" : "Salt Lake - 2",
"Pincode" : "700093"
}
]
}
🟩Input : ($unwind)
db.students.aggregate([{$unwind : "$Address"},{$match : {StudentNo: "2"}}, {$project: {"Address":1, "FirstName":1}}])
🟩Output : ($unwind)
{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"FirstName" : "Tax",
"Address" : {
"City" : "Salt Lake - 1",
"Pincode" : "700092"
}
}
{
"_id" : ObjectId("65d597481172d5d9824698fe"),
"FirstName" : "Tax",
"Address" : {
"City" : "Salt Lake - 2",
"Pincode" : "700093"
}
}
🔴$lookup is an aggregation pipeline stage that allows you to perform a left outer join between two collections
users collection
{
"_id" : ObjectId("65d5e1ae768e0e2b126763f9"),
"name" : "Rahul",
"dept" : "Tech"
}
{
"_id" : ObjectId("65d5e1ae768e0e2b126763fa"),
"name" : "Nisha",
"dept" : "HR"
}
{
"_id" : ObjectId("65d5e1ae768e0e2b126763fb"),
"name" : "Ram",
"dept" : "Tech"
}
department collection
{
"_id" : ObjectId("65d5e118768e0e2b126763f0"),
"name" : "Tech",
"code" : "T9021"
}
{
"_id" : ObjectId("65d5e118768e0e2b126763f1"),
"name" : "HR",
"code" : "H9075"
}
{
"_id" : ObjectId("65d5e118768e0e2b126763f2"),
"name" : "DevOps",
"code" : "D9080"
}
👇👇query using lookup(LEFT OUTER JOIN)👇👇
Note: using lookup we can achieve left outer join. Here users(left collection) and department(right collection), the common field between this two table is dept(users) and name(department). So it will fetch all the data from the users table/collection and the common field between users and department table and show the data in the field name "anything"
db.users.aggregate({
$lookup: {
from: "department",
localField: "dept",
foreignField: "name",
as: "anything",
}
})
🟩Output :
{
"_id" : ObjectId("65d5e1ae768e0e2b126763f9"),
"name" : "Rahul",
"dept" : "Tech",
"anything" : [
{
"_id" : ObjectId("65d5e118768e0e2b126763f0"),
"name" : "Tech",
"code" : "T9021"
}
]
}
{
"_id" : ObjectId("65d5e1ae768e0e2b126763fa"),
"name" : "Nisha",
"dept" : "HR",
"anything" : [
{
"_id" : ObjectId("65d5e118768e0e2b126763f1"),
"name" : "HR",
"code" : "H9075"
}
]
}
{
"_id" : ObjectId("65d5e1ae768e0e2b126763fb"),
"name" : "Ram",
"dept" : "Tech",
"anything" : [
{
"_id" : ObjectId("65d5e118768e0e2b126763f0"),
"name" : "Tech",
"code" : "T9021"
}
]
}
▶Right Outer Join :
db.department.aggregate({
$lookup: {
from: "users",
localField: "name",
foreignField: "dept",
as: "anything",
}
})
🟧Output :
{
"_id" : ObjectId("65d5e118768e0e2b126763f0"),
"name" : "Tech",
"code" : "T9021",
"anything" : [
{
"_id" : ObjectId("65d5e1ae768e0e2b126763f9"),
"name" : "Rahul",
"dept" : "Tech"
},
{
"_id" : ObjectId("65d5e1ae768e0e2b126763fb"),
"name" : "Ram",
"dept" : "Tech"
}
]
}
{
"_id" : ObjectId("65d5e118768e0e2b126763f1"),
"name" : "HR",
"code" : "H9075",
"anything" : [
{
"_id" : ObjectId("65d5e1ae768e0e2b126763fa"),
"name" : "Nisha",
"dept" : "HR"
}
]
}
{
"_id" : ObjectId("65d5e118768e0e2b126763f2"),
"name" : "DevOps",
"code" : "D9080",
"anything" : [
]
}
▶Inner Join :
db.users.aggregate([
{
$lookup: {
from: "department",
localField: "dept",
foreignField: "name",
as: "departmentInfo"
}
},
{
$project: {
"name": 1,
"dept": 1,
"departmentInfo.code": 1,
"_id": 0
}
}
]);
➡Output :
{
"name" : "Rahul",
"dept" : "Tech",
"departmentInfo" : [
{
"code" : "T9021"
}
]
}
{
"name" : "Nisha",
"dept" : "HR",
"departmentInfo" : [
{
"code" : "H9075"
}
]
}
{
"name" : "Ram",
"dept" : "Tech",
"departmentInfo" : [
{
"code" : "T9021"
}
]
}
MongoDB (Database -> Collection -> Document)
1. Create DB
use mydb
2. Show DBs
show dbs
3. Current DB
db
4. Drop DB
db.dropDatabase()
5. Create Collection Method-1 :
db.createCollection("myCollection")
Method-2 :db.myCollection2.insertOne({"name":"Rajesh"})
6. Show Collection
show collections
7. Drop Collection
db.myCollection.drop()
8.Insert Data in Document
db.students.insertOne({ "StudentNo" : "1", "FirstName" : "Max", "LastName" : "Joe", "Age" : "10" })
9.Insert Many Data in Document
db.students.insertMany([{ "StudentNo" : "2", "FirstName" : "Tax", "LastName" : "Koe", "Age" : "9" },{ "StudentNo" : "3", "FirstName" : "Fax", "LastName" : "Loe", "Age" : "10.4" }])
10. Show data from document
db.students.find()