Skip to content

Commit de2877b

Browse files
committed
Notes
1 parent c8bae7b commit de2877b

File tree

2 files changed

+342
-2
lines changed

2 files changed

+342
-2
lines changed

Notes.txt

Whitespace-only changes.

README.md

+342-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,342 @@
1-
# mongodb
2-
Practicing MongoDB
1+
---
2+
description: >-
3+
In my MongoDB documentation, I am practicing various operations and commands
4+
to interact with a MongoDB database named "school." Here's a breakdown of the
5+
context for each section of my documentation
6+
---
7+
8+
# MongoDB Documentation
9+
### In my MongoDB documentation, I am practicing various operations and commands to interact with a MongoDB database named "school." Here's a breakdown of the context for each section of my documentation
10+
11+
## Connection:
12+
13+
* Connection string: `mongosh "mongodb+srv://cluster0.k3uephw.mongodb.net/" --apiVersion 1 --username mayur`
14+
* Password: JE5lyxzuYC58XHWs
15+
16+
## Database:
17+
18+
* Show available databases: `show dbs`
19+
* Switch to the "school" database: `use school`
20+
* Create a collection named "students": `db.createCollection("students")`
21+
* Drop the "school" database: `db.dropDatabase()`
22+
23+
## Insert:
24+
25+
* Insert a single document:
26+
27+
```javascript
28+
javascriptCopy codedb.students.insertOne({name: "Mayur Khadde", age: 30, gpa: 3.2})
29+
```
30+
31+
32+
* Insert multiple documents:
33+
34+
```javascript
35+
javascriptCopy codedb.students.insertMany([
36+
{name: "Patrick", age: 38, gpa: 1.5},
37+
{name: "Sandy", age: 27, gpa: 4.0},
38+
{name: "Gary", age: 18, gpa: 2.5}
39+
])
40+
```
41+
42+
43+
* Insert a complex document:
44+
45+
```javascript
46+
javascriptCopy codedb.students.insertOne({
47+
name: "Larry 123",
48+
age: 32,
49+
gpa: 2.8,
50+
fullTime: true,
51+
registerDate: new Date("2023-01-02"),
52+
gradutionDate: null,
53+
courses: ["Biology", "Chemistry", "Calculus"],
54+
address: {
55+
Street: "123 Fake St.",
56+
city: "Bikini Bottom",
57+
zip: 12345
58+
}
59+
})
60+
```
61+
62+
## Find:
63+
64+
* Find all students, sorted by name in descending order:
65+
66+
```javascript
67+
javascriptCopy codedb.students.find().sort({name: -1})
68+
```
69+
70+
71+
* Find all students, sorted by GPA in ascending order:
72+
73+
```javascript
74+
javascriptCopy codedb.students.find().sort({gpa: 1})
75+
```
76+
77+
78+
* Find the first student:
79+
80+
```javascript
81+
javascriptCopy codedb.students.find().limit(1)
82+
```
83+
84+
85+
* Find the student with the highest GPA:
86+
87+
```javascript
88+
javascriptCopy codedb.students.find().sort({gpa: -1}).limit(1)
89+
```
90+
91+
92+
* Find a student by name:
93+
94+
```javascript
95+
javascriptCopy codedb.students.find({name: "Mayur"})
96+
```
97+
98+
99+
* Find full-time students:
100+
101+
```javascript
102+
javascriptCopy codedb.students.find({fullTime: true})
103+
```
104+
105+
106+
* Find all students, displaying only their names:
107+
108+
```javascript
109+
javascriptCopy codedb.students.find({}, {name: true})
110+
```
111+
112+
113+
* Find all students, excluding the `_id` field, and displaying only their names:
114+
115+
```javascript
116+
javascriptCopy codedb.students.find({}, {_id: false, name: true})
117+
```
118+
119+
## Update:
120+
121+
* Update "Gary" to be a full-time student:
122+
123+
```javascript
124+
javascriptCopy codedb.students.updateOne({ name: "Gary" }, { $set: { fullTime: true } })
125+
```
126+
127+
128+
* Find "Gary" to confirm the update:
129+
130+
```javascript
131+
javascriptCopy codedb.students.find({name: "Gary"})
132+
```
133+
134+
135+
* Update a document by its `_id`:
136+
137+
```javascript
138+
javascriptCopy codedb.students.updateOne({ _id: ObjectId("6547d516fa269bcad3447d35") }, { $set: { fullTime: false } })
139+
```
140+
141+
142+
* Unset the "fullTime" field in a document:
143+
144+
```javascript
145+
javascriptCopy codedb.students.updateOne({ _id: ObjectId("6547d516fa269bcad3447d35") }, { $unset: { fullTime: "" } })
146+
```
147+
148+
149+
* Update all students to be non-full-time:
150+
151+
```javascript
152+
javascriptCopy codedb.students.updateMany({}, { $set: { fullTime: false } })
153+
```
154+
155+
156+
* Unset the "fullTime" field for a student named "Tushar":
157+
158+
```javascript
159+
javascriptCopy codedb.students.updateOne({ name: "Tushar" }, { $unset: { fullTime: "" } })
160+
```
161+
162+
163+
* Find the "Tushar" student to confirm the update:
164+
165+
```javascript
166+
javascriptCopy codedb.students.find({ name: "Tushar" })
167+
```
168+
169+
170+
* Update all students without a "fullTime" field to be full-time:
171+
172+
```javascript
173+
javascriptCopy codedb.students.updateMany({ fullTime: { $exists: false } }, { $set: { fullTime: true } })
174+
```
175+
176+
177+
* Find the "Tushar" student again to confirm the update:
178+
179+
```javascript
180+
javascriptCopy codedb.students.find({ name: "Tushar" })
181+
```
182+
183+
## Delete:
184+
185+
* Delete a single document by name:
186+
187+
```javascript
188+
javascriptCopy codedb.students.deleteOne({ name: "Tushar" })
189+
```
190+
191+
192+
* Confirm that the "Tushar" student is deleted:
193+
194+
```javascript
195+
javascriptCopy codedb.students.find({ name: "Tushar" })
196+
```
197+
198+
199+
* Delete all non-full-time students:
200+
201+
```javascript
202+
javascriptCopy codedb.students.deleteMany({ fullTime: false })
203+
```
204+
205+
## Comparison Operator:
206+
207+
* Find students with names other than "Mayur":
208+
209+
```javascript
210+
javascriptCopy codedb.students.find({ name: { $ne: "Mayur" } })
211+
```
212+
213+
214+
* Find students with ages less than or equal to 30:
215+
216+
```javascript
217+
javascriptCopy codedb.students.find({ age: { $lte: 30 } })
218+
```
219+
220+
221+
* Find students with ages greater than or equal to 27:
222+
223+
```javascript
224+
javascriptCopy codedb.students.find({ age: { $gte: 27 } })
225+
```
226+
227+
228+
* Find students with GPAs between 3 and 4:
229+
230+
```javascript
231+
javascriptCopy codedb.students.find({ gpa: { $gte: 3, $lte: 4 } })
232+
```
233+
234+
235+
* Find students with names "Mayur" or "Gary":
236+
237+
```javascript
238+
javascriptCopy codedb.students.find({ name: { $in: ["Mayur", "Gary"] } })
239+
```
240+
241+
242+
* Find students with names other than "Mayur" or "Gary":
243+
244+
```javascript
245+
javascriptCopy codedb.students.find({ name: { $nin: ["Mayur", "Gary"] } })
246+
```
247+
248+
## Logical Operator:
249+
250+
* Find full-time students aged 22 or younger (AND condition):
251+
252+
```javascript
253+
javascriptCopy codedb.students.find({ $and: [{ fullTime: true }, { age: { $lte: 22 } }] })
254+
```
255+
256+
257+
* Find students older than 30 (NOT condition):
258+
259+
```javascript
260+
javascriptCopy codedb.students.find({ age: { $not: { $gte: 30 } } })
261+
```
262+
263+
264+
* Find non-full-time students or students aged 22 or younger (NOR condition):
265+
266+
```javascript
267+
javascriptCopy codedb.students.find({ $nor: [{ fullTime: true }, { age: { $lte: 22 } }] })
268+
```
269+
270+
271+
* Find full-time students or students aged 22 or younger (OR condition):
272+
273+
```javascript
274+
javascriptCopy codedb.students.find({ $or: [{ fullTime: true }, { age: { $lte: 22 } }] })
275+
```
276+
277+
## Index:
278+
279+
* Perform a linear search for a student named "Larry":
280+
281+
```javascript
282+
javascriptCopy codedb.students.find({ name: "Larry" }).explain("executionStats")
283+
```
284+
285+
286+
* Create an index on the "name" field:
287+
288+
```javascript
289+
javascriptCopy codedb.students.createIndex({ name: 1 })
290+
```
291+
292+
293+
* Perform the same search with the new index:
294+
295+
```javascript
296+
javascriptCopy codedb.students.find({ name: "Larry" }).explain("executionStats")
297+
```
298+
299+
300+
* Get the list of indexes on the "students" collection:
301+
302+
```javascript
303+
javascriptCopy codedb.students.getIndexes()
304+
```
305+
306+
307+
* Drop the "name" index:
308+
309+
```javascript
310+
javascriptCopy codedb.students.dropIndex("name_1")
311+
```
312+
313+
## Collections:
314+
315+
* Show all collections in the current database:
316+
317+
```javascript
318+
javascriptCopy codeshow collections
319+
```
320+
321+
322+
* Create a capped collection named "teacher" with specified options:
323+
324+
```javascript
325+
javascriptCopy codedb.createCollection("teacher", { capped: true, size: 10000000, max: 100, autoIndexId: false })
326+
```
327+
328+
329+
* Create a regular collection named "courses":
330+
331+
```javascript
332+
javascriptCopy codedb.createCollection("courses")
333+
```
334+
335+
336+
* Drop the "courses" collection:
337+
338+
```javascript
339+
javascriptCopy codedb.courses.drop()
340+
```
341+
342+
Remember to keep your documentation up to date and organized for easy reference in the future.

0 commit comments

Comments
 (0)