Skip to content

Commit a8ba311

Browse files
committed
Added JavaScript and mongosh tabs to challenge solutions
1 parent 5aabe5d commit a8ba311

File tree

10 files changed

+343
-113
lines changed

10 files changed

+343
-113
lines changed

docs/40-CRUD/1-WHERE.mdx

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
14
# 👐 WHERE → .find()
25

36
Similar to SQL's `WHERE` clause, the `.find()` method in MongoDB retrieves documents from a collection that matches a specified query.
@@ -91,31 +94,77 @@ Now, translate the following into a MongoDB query.
9194

9295
<details>
9396
<summary>Answer</summary>
94-
<div>
95-
```js
96-
db.books.find({ totalInventory: 5 });
97-
```
98-
</div>
97+
<Tabs groupId="challenges" defaultValue={"javascript"}>
98+
<TabItem value="javascript" label="JavaScript">
99+
<div>
100+
```js
101+
const cursor = await books.find({ totalInventory: 5 });
102+
103+
await cursor.forEach((b) => {
104+
console.log(b);
105+
});
106+
```
107+
</div>
108+
</TabItem>
109+
<TabItem value="mongosh" label="mongosh">
110+
<div>
111+
```js
112+
db.books.find({ totalInventory: 5 });
113+
```
114+
</div>
115+
</TabItem>
116+
</Tabs>
117+
99118
</details>
100119

101120
#### 2. Find all books with more than 300 pages.
102121

103122
<details>
104123
<summary>Answer</summary>
105-
<div>
106-
```js
107-
db.books.find({ pages: {$gt: 300} });
108-
```
109-
</div>
124+
<Tabs groupId="challenges" defaultValue={"javascript"}>
125+
<TabItem value="javascript" label="JavaScript">
126+
<div>
127+
```js
128+
const cursor = await books.find({ pages: {$gt: 300} });
129+
130+
await cursor.forEach((b) => {
131+
console.log(b);
132+
});
133+
```
134+
</div>
135+
</TabItem>
136+
<TabItem value="mongosh" label="mongosh">
137+
<div>
138+
```js
139+
db.books.find({ pages: {$gt: 300} });
140+
```
141+
</div>
142+
</TabItem>
143+
</Tabs>
110144
</details>
111145

112146
#### 3. Find books in the `Science` genre that are more than 300 pages long.
113147

114148
<details>
115149
<summary>Answer</summary>
116-
<div>
117-
```js
118-
db.books.find({ "genre.name": "Science", pages: {$gt: 300} });
119-
```
120-
</div>
150+
<Tabs groupId="challenges" defaultValue={"javascript"}>
151+
<TabItem value="javascript" label="JavaScript">
152+
<div>
153+
```js
154+
const cursor = await books.find( { "genre.name": "Science", pages: {$gt: 300} } );
155+
156+
await cursor.forEach((b) => {
157+
console.log(b);
158+
});
159+
```
160+
</div>
161+
</TabItem>
162+
<TabItem value="mongosh" label="mongosh">
163+
<div>
164+
```js
165+
db.books.find({ "genre.name": "Science", pages: {$gt: 300} });
166+
```
167+
</div>
168+
</TabItem>
169+
</Tabs>
121170
</details>

docs/40-CRUD/2-SELECT.mdx

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
14
# 👐 SELECT → projection
25

36
In SQL, the `SELECT` statement allows us to specify which columns to retrieve from a table. Similarly, in MongoDB, we use **projection** in the `.find()` method to control which fields to include (or exclude) in query results.
@@ -77,20 +80,51 @@ Here:
7780

7881
<details>
7982
<summary>Answer</summary>
80-
<div>
81-
```js
82-
db.books.find({}, {title: 1, _id: 0});
83-
```
84-
</div>
83+
<Tabs groupId="challenges" defaultValue={"javascript"}>
84+
<TabItem value="javascript" label="JavaScript">
85+
<div>
86+
```js
87+
const cursor = await books.find({}).project( {title: 1, _id: 0} ).limit(10);
88+
89+
await cursor.forEach((b) => {
90+
console.log(b);
91+
});
92+
```
93+
</div>
94+
95+
</TabItem>
96+
<TabItem value="mongosh" label="mongosh">
97+
<div>
98+
```js
99+
db.books.find({}, {title: 1, _id: 0});
100+
```
101+
</div>
102+
</TabItem>
103+
</Tabs>
85104
</details>
86105

87106
### 👐 2. Retrieve all fields except `_id` and `authors` for books in the "History" genre.
88107

89108
<details>
90109
<summary>Answer</summary>
91-
<div>
92-
```js
93-
db.books.find({ "genre.name": "History" }, { _id: 0, authors: 0 });
94-
```
95-
</div>
110+
<Tabs groupId="challenges" defaultValue={"javascript"}>
111+
<TabItem value="javascript" label="JavaScript">
112+
<div>
113+
```js
114+
const cursor = await books.find({ "genre.name": "History" }).project( { _id: 0, authors: 0 } ).limit(10);
115+
116+
await cursor.forEach((b) => {
117+
console.log(b);
118+
});
119+
```
120+
</div>
121+
</TabItem>
122+
<TabItem value="mongosh" label="mongosh">
123+
<div>
124+
```js
125+
db.books.find({ "genre.name": "History" }, { _id: 0, authors: 0 });
126+
```
127+
</div>
128+
</TabItem>
129+
</Tabs>
96130
</details>

docs/40-CRUD/3-ORDER-LIMIT.mdx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
14
# 👐 ORDER BY → .sort() & LIMIT → .limit()
25

36
In SQL, we use `ORDER BY` to sort query results and `LIMIT` to restrict the number of returned rows. MongoDB provides the `.sort()` and `.limit()` methods to achieve the same.
@@ -64,9 +67,24 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
6467

6568
<details>
6669
<summary>Answer</summary>
67-
<div>
68-
```js
69-
db.books.find({}).sort({title: 1}).limit(10)
70-
```
71-
</div>
70+
<Tabs groupId="challenges" defaultValue={"javascript"}>
71+
<TabItem value="javascript" label="JavaScript">
72+
<div>
73+
```js
74+
const cursor = await books.find({}).sort({title: 1}).limit(10);
75+
76+
await cursor.forEach((b) => {
77+
console.log(b);
78+
});
79+
```
80+
</div>
81+
</TabItem>
82+
<TabItem value="mongosh" label="mongosh">
83+
<div>
84+
```js
85+
db.books.find({}).sort({title: 1}).limit(10)
86+
```
87+
</div>
88+
</TabItem>
89+
</Tabs>
7290
</details>

docs/40-CRUD/4-INSERT-DELETE.mdx

Lines changed: 87 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
14
# 👐 INSERT → insertOne() & DELETE → deleteOne()
25

36
MongoDB provides two methods for inserting documents into a collection:
@@ -67,45 +70,94 @@ DELETE FROM reviews WHERE bookId = '0786222727';
6770

6871
<details>
6972
<summary>Answer</summary>
70-
<div>
71-
```js
72-
db.reviews.insertMany([
73-
{
74-
text: "Thrilling end.",
75-
rating: 4,
76-
name: "Mark",
77-
bookId: "0786222727",
78-
},
79-
{
80-
text: "Must read!",
81-
rating: 5,
82-
name: "Raj",
83-
bookId: "0786222727",
84-
},
85-
{
86-
text: "Very expensive",
87-
rating: 3,
88-
name: "Yun",
89-
bookId: "0786222727",
90-
},
91-
{
92-
text: "Extremely satisfied with the storyline!",
93-
rating: 5,
94-
name: "Lisa",
95-
bookId: "0786222727",
96-
}
97-
]);
98-
```
99-
</div>
73+
<Tabs groupId="challenges" defaultValue={"javascript"}>
74+
<TabItem value="javascript" label="JavaScript">
75+
<div>
76+
```js
77+
const reviews = db.collection("reviews");
78+
await reviews.insertMany([
79+
{
80+
text: "Thrilling end.",
81+
rating: 4,
82+
name: "Mark",
83+
bookId: "0786222727",
84+
},
85+
{
86+
text: "Must read!",
87+
rating: 5,
88+
name: "Raj",
89+
bookId: "0786222727",
90+
},
91+
{
92+
text: "Very expensive",
93+
rating: 3,
94+
name: "Yun",
95+
bookId: "0786222727",
96+
},
97+
{
98+
text: "Extremely satisfied with the storyline!",
99+
rating: 5,
100+
name: "Lisa",
101+
bookId: "0786222727",
102+
}
103+
]);
104+
```
105+
</div>
106+
</TabItem>
107+
<TabItem value="mongosh" label="mongosh">
108+
<div>
109+
```js
110+
db.reviews.insertMany([
111+
{
112+
text: "Thrilling end.",
113+
rating: 4,
114+
name: "Mark",
115+
bookId: "0786222727",
116+
},
117+
{
118+
text: "Must read!",
119+
rating: 5,
120+
name: "Raj",
121+
bookId: "0786222727",
122+
},
123+
{
124+
text: "Very expensive",
125+
rating: 3,
126+
name: "Yun",
127+
bookId: "0786222727",
128+
},
129+
{
130+
text: "Extremely satisfied with the storyline!",
131+
rating: 5,
132+
name: "Lisa",
133+
bookId: "0786222727",
134+
}
135+
]);
136+
```
137+
</div>
138+
</TabItem>
139+
</Tabs>
100140
</details>
101141

102142
### 👐 2. Delete all the reviews for `bookId` "0786222727" through a single command.
103143

104144
<details>
105145
<summary>Answer</summary>
106-
<div>
107-
```js
108-
db.reviews.deleteMany({"bookId": "0786222727"})
109-
```
110-
</div>
146+
<Tabs groupId="challenges" defaultValue={"javascript"}>
147+
<TabItem value="javascript" label="JavaScript">
148+
<div>
149+
```js
150+
const reviews = db.collection("reviews");
151+
await reviews.deleteMany({"bookId": "0786222727"})
152+
```
153+
</div>
154+
</TabItem>
155+
<TabItem value="mongosh" label="mongosh">
156+
<div>
157+
```js
158+
db.reviews.deleteMany({"bookId": "0786222727"})
159+
```
160+
</div>
161+
</TabItem>
162+
</Tabs>
111163
</details>

docs/40-CRUD/5-UPDATE.mdx

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
14
# 👐 UPDATE → updateOne(), updateMany()
25

36
To modify existing documents, MongoDB provides:
@@ -82,12 +85,26 @@ Executing the above command will insert a fresh new document in the collection,
8285

8386
<details>
8487
<summary>Answer</summary>
85-
<div>
86-
```js
87-
db.books.updateOne(
88-
{"title": "Treasure of the Sun"},
89-
{$set: {pages: 449}}
90-
);
91-
```
92-
</div>
88+
<Tabs groupId="challenges" defaultValue={"javascript"}>
89+
<TabItem value="javascript" label="JavaScript">
90+
<div>
91+
```js
92+
await books.updateOne(
93+
{"title": "Treasure of the Sun"},
94+
{$set: {pages: 449}}
95+
);
96+
```
97+
</div>
98+
</TabItem>
99+
<TabItem value="mongosh" label="mongosh">
100+
<div>
101+
```js
102+
db.books.updateOne(
103+
{"title": "Treasure of the Sun"},
104+
{$set: {pages: 449}}
105+
);
106+
```
107+
</div>
108+
</TabItem>
109+
</Tabs>
93110
</details>

0 commit comments

Comments
 (0)