-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLectures.txt
More file actions
158 lines (127 loc) · 4.72 KB
/
Lectures.txt
File metadata and controls
158 lines (127 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
MongoDB:
1. A document oriented non-relational NoSQL Database.
2. Store data in a document
- Each document has field (key-value apirs)
- Each document is a json object
3. Flexibility
- Schema less
4. Performance
-> Indexing
-> Embeded documents
-> Sherding
{
"_id": 1,
'name": "Zakir",
"email": "zakir@gmail.com",
"courses": ["WAF", "CC", "CG"].
courseInfo: [{"_id": 1, "title": "Web A..", ...},{"":"", "":"", ...}]-> emebedding documents, Nexted document objects.
}
5. Free and Open source
- Internally it is in BSON Format.
Commands:
- show MongoDB
- use dbname
- db-courses.find()
- db-courses.insert()
Steps:
Connect to MongoDB
Create a new database
Create a collection
Insert documents (Create)
Read documents (Retrieve)
Update documents
Delete documents
1. Create Database
- use myDatabase
2. Create a Collection
- db.createCollection("myCollection")
3. Insert document in the Database
- db.myCollection.insertOne({ name: "John Doe", age: 25, profession: "Engineer" })
- To insert multiple objects:
db.myCollection.insertMany([
{ name: "Alice", age: 30, profession: "Doctor" },
{ name: "Bob", age: 22, profession: "Designer" }
])
4. View the Documents in the Collection
- db.myCollection.find()
- db.myCollection.find().pretty()
Read specific data:
- for Specific Data:
- db.myCollection.find({ name: "Alice" })
- to update one document:
- db.myCollection.updateOne(
{ name: "Alice" },
{ $set: { age: 26 } }
)
- to pdate multiple documents:
- db.myCollection.updateMany(
{ profession: "Designer" },
{ $set: { profession: "Graphic Designer" } }
)
- to delete one document:
- db.myCollection.deleteOne({ name: "Charlie" })
- to delete multiple documents:
- db.myCollection.deleteMany({ profession: "Graphic Designer" })
5. Check existing Database and Collections
- show databases
- show collections
6. Find documents where name is "Alice" OR age is greater than 25.
- db.myCollection.find({
$or: [
{ name: "Alice" },
{ age: { $gt: 25 } }
]
})
7. AND Operator
db.myCollection.find({
$and: [
{ name: "Alice" },
{ age: { $gt: 25 } }
]
})
8. Find documents where name is "Alice" AND age is greater than 25.
db.myCollection.find({
$and: [
{ name: "Alice" },
{ age: { $gt: 25 } }
]
})
9. where age is less than 25
db.myCollection.find({ age: { $lt: 25 } })
10. Find documents where age is not equal to 30.
db.myCollection.find({ age: { $ne: 30 } })
11. To add a field and return the updated document:
db.users.findOneAndUpdate(
{ _id: ObjectId("your_document_id") }, // Filter
{ $set: { isVerified: true } }, // New field
{ returnDocument: "after" } // Returns the updated document
)
12. Update many documents
db.users.updateMany(
{ organization: "QAU" }, // Filter criteria
{ $set: { status: "active" } } // New field and value
)
13. Add or update multiple fields at once for all matching documents:
db.users.updateMany(
{ department: "Computer Science" }, // Filter criteria
{ $set: { status: "active", lastUpdated: new Date() } } // Fields to update
)
14. Remove the temporaryField from all matching documents:
db.users.updateMany(
{ organization: "QAU" }, // Filter
{ $unset: { temporaryField: "" } } // Remove field
)
15. Remove Multiple Fields at Once
db.users.updateMany(
{ department: "Computer Science" }, // Filter criteria
{ $unset: { temporaryField1: "", temporaryField2: "" } } // Fields to remove
)
16. Remove a Field from a Single Document
db.users.updateOne(
{ _id: ObjectId("your_document_id") }, // Filter to find the document
{ $unset: { temporaryField: "" } } // Field to remove
)
-> Mongoose
- an object data modelling(ODM) library for MongoDB and Nodejs providing highet level of abstraction.
- Schema is created to model data.
-