-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperators
More file actions
170 lines (162 loc) ยท 4.74 KB
/
Operators
File metadata and controls
170 lines (162 loc) ยท 4.74 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
159
160
161
162
163
164
165
166
167
168
169
170
LOGICAL OPERATORS ๐๐๐
codeio> db.course.find({ $and:[{videos:35},{name:"React"}]}) //$and is syntax write like within square bracket placed it, it work like And operator๐๐ฉต
[
{
_id: ObjectId('68d2d5e15ed1a5518ccebea4'),
name: 'React',
videos: 35,
duration: 18.3,
PublishedDate: ISODate('2025-09-23T00:00:00.000Z'),
instructors: [ 'Goms', 'Bob' ],
relatedCourses: { name: 'js', videos: 36, duration: 11 },
completed: true
}
]
codeio> db.course.find({ $or:[{videos:35},{name:"React"}]}) //$or ,Same for OR operator๐๐งก
[
{
_id: ObjectId('68d2cac1488f2d29f7cebea4'),
name: 'DSA',
videos: 35,
duration: 28.5,
completed: true
},
{
_id: ObjectId('68d2d5e15ed1a5518ccebea4'),
name: 'React',
videos: 35,
duration: 18.3,
PublishedDate: ISODate('2025-09-23T00:00:00.000Z'),
instructors: [ 'Goms', 'Bob' ],
relatedCourses: { name: 'js', videos: 36, duration: 11 },
completed: true
}
]
codeio> db.course.find({ $nor:[{videos:35},{name:"React"}]}) //And also same for NOR operator๐๐
[
{
_id: ObjectId('68d2cbdb488f2d29f7cebea7'),
name: 'JAVA',
videos: 50,
duration: 10.5,
completed: true
},
{
_id: ObjectId('68d2cbdb488f2d29f7cebea8'),
name: 'C++',
videos: 36,
duration: 10.51,
completed: true
},
{
_id: ObjectId('68d2d6615ed1a5518ccebea5'),
name: 'Mongo DB',
videos: 15,
duration: 2.5,
completed: true
},
{
_id: ObjectId('68d4182cc5e0eb2fcbcebea5'),
name: 'c++',
videos: 50,
duration: 10.5,
completed: true
},
{
_id: ObjectId('68d5792c74d91a0aa6af9f6c'),
name: 'Spring Boot',
duration: 6.7,
videos: 50
}
]
๐๐๐๐๐(Recap)
ย db.course.find({ $and:[{videos:35},{name:"React"}]})
db.course.find({ $or:[{videos:35},{name:"React"}]})
db.course.find({ $nor:[{videos:35},{name:"React"}]})
๐๐๐๐๐
ARRAY OPRATOR๐ฅ๐ฅ๐ฅ
codeio> db.course.updateOne({name:"React"},{$push:{instructors:"Goms"}}) //If you insert element in array ,you can use the following operators. $push operator mean that insert element in array๐ซ
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
codeio> db.course.find({name:"React"})
[
{
_id: ObjectId('68d2d5e15ed1a5518ccebea4'),
name: 'React',
videos: 35,
duration: 18.3,
PublishedDate: ISODate('2025-09-23T00:00:00.000Z'),
instructors: [ 'Goms', 'Bob', 'Goms' ],
relatedCourses: { name: 'js', videos: 36, duration: 11 },
completed: true
}
]
codeio> db.course.updateOne({name:"React"},{$addToSet:{instructors:"Goms"}}) //$addToSet means that yis also insert but only difference is it doesn't insert duplicate๐ฉต
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 0, //you can see no modified๐
upsertedCount: 0
}
codeio> db.course.updateOne({name:"React"},{$addToSet:{instructors:"Codeio"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1, //This case no duplicate ,so insert it show modifiedcount =1๐
upsertedCount: 0
}
codeio> db.course.find({name:"React"})
[
{
_id: ObjectId('68d2d5e15ed1a5518ccebea4'),
name: 'React',
videos: 35,
duration: 18.3,
PublishedDate: ISODate('2025-09-23T00:00:00.000Z'),
instructors: [ 'Goms', 'Bob', 'Goms', 'Codeio' ],
relatedCourses: { name: 'js', videos: 36, duration: 11 },
completed: true
}
]
codeio> db.course.updateOne({name:"React"},{$pull:{instructors:"Codeio"}}) //$pull means that delete element ๐
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
codeio> db.course.updateOne({name:"React"},{$pop:{instructors:-1}}) //$pop use can delete with elements name or give -1 delete 1'st element This opposite is 1 delete last element๐ค
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
codeio> db.course.find({name:"React"})
[
{
_id: ObjectId('68d2d5e15ed1a5518ccebea4'),
name: 'React',
videos: 35,
duration: 18.3,
PublishedDate: ISODate('2025-09-23T00:00:00.000Z'),
instructors: [ 'Bob', 'Goms' ],
relatedCourses: { name: 'js', videos: 36, duration: 11 },
completed: true
}
]
๐๐๐๐(Recap)
codeio> db.course.updateOne({name:"React"},{$push:{instructors:"Goms"}})
db.course.updateOne({name:"React"},{$addToSet:{instructors:"Goms"}})
db.course.updateOne({name:"React"},{$pull:{instructors:"Codeio"}})
db.course.updateOne({name:"React"},{$pop:{instructors:-1}})
๐๐๐๐