Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 64 additions & 15 deletions docs/40-CRUD/1-WHERE.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# 👐 WHERE → .find()

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

<details>
<summary>Answer</summary>
<div>
```js
db.books.find({ totalInventory: 5 });
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
const cursor = await books.find({ totalInventory: 5 });

await cursor.forEach((b) => {
console.log(b);
});
```
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.books.find({ totalInventory: 5 });
```
</div>
</TabItem>
</Tabs>

</details>

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

<details>
<summary>Answer</summary>
<div>
```js
db.books.find({ pages: {$gt: 300} });
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
const cursor = await books.find({ pages: {$gt: 300} });

await cursor.forEach((b) => {
console.log(b);
});
```
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.books.find({ pages: {$gt: 300} });
```
</div>
</TabItem>
</Tabs>
</details>

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

<details>
<summary>Answer</summary>
<div>
```js
db.books.find({ "genre.name": "Science", pages: {$gt: 300} });
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
const cursor = await books.find( { "genre.name": "Science", pages: {$gt: 300} } );

await cursor.forEach((b) => {
console.log(b);
});
```
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.books.find({ "genre.name": "Science", pages: {$gt: 300} });
```
</div>
</TabItem>
</Tabs>
</details>
54 changes: 44 additions & 10 deletions docs/40-CRUD/2-SELECT.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# 👐 SELECT → projection

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.
Expand Down Expand Up @@ -77,20 +80,51 @@ Here:

<details>
<summary>Answer</summary>
<div>
```js
db.books.find({}, {title: 1, _id: 0});
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
const cursor = await books.find({}).project( {title: 1, _id: 0} ).limit(10);

await cursor.forEach((b) => {
console.log(b);
});
```
</div>

</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.books.find({}, {title: 1, _id: 0});
```
</div>
</TabItem>
</Tabs>
</details>

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

<details>
<summary>Answer</summary>
<div>
```js
db.books.find({ "genre.name": "History" }, { _id: 0, authors: 0 });
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
const cursor = await books.find({ "genre.name": "History" }).project( { _id: 0, authors: 0 } ).limit(10);

await cursor.forEach((b) => {
console.log(b);
});
```
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.books.find({ "genre.name": "History" }, { _id: 0, authors: 0 });
```
</div>
</TabItem>
</Tabs>
</details>
28 changes: 23 additions & 5 deletions docs/40-CRUD/3-ORDER-LIMIT.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# 👐 ORDER BY → .sort() & LIMIT → .limit()

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.
Expand Down Expand Up @@ -64,9 +67,24 @@ This returns the **top 10 available books** in the "Science Fiction" genre.

<details>
<summary>Answer</summary>
<div>
```js
db.books.find({}).sort({title: 1}).limit(10)
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
const cursor = await books.find({}).sort({title: 1}).limit(10);

await cursor.forEach((b) => {
console.log(b);
});
```
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.books.find({}).sort({title: 1}).limit(10)
```
</div>
</TabItem>
</Tabs>
</details>
122 changes: 87 additions & 35 deletions docs/40-CRUD/4-INSERT-DELETE.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# 👐 INSERT → insertOne() & DELETE → deleteOne()

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

<details>
<summary>Answer</summary>
<div>
```js
db.reviews.insertMany([
{
text: "Thrilling end.",
rating: 4,
name: "Mark",
bookId: "0786222727",
},
{
text: "Must read!",
rating: 5,
name: "Raj",
bookId: "0786222727",
},
{
text: "Very expensive",
rating: 3,
name: "Yun",
bookId: "0786222727",
},
{
text: "Extremely satisfied with the storyline!",
rating: 5,
name: "Lisa",
bookId: "0786222727",
}
]);
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
const reviews = db.collection("reviews");
await reviews.insertMany([
{
text: "Thrilling end.",
rating: 4,
name: "Mark",
bookId: "0786222727",
},
{
text: "Must read!",
rating: 5,
name: "Raj",
bookId: "0786222727",
},
{
text: "Very expensive",
rating: 3,
name: "Yun",
bookId: "0786222727",
},
{
text: "Extremely satisfied with the storyline!",
rating: 5,
name: "Lisa",
bookId: "0786222727",
}
]);
```
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.reviews.insertMany([
{
text: "Thrilling end.",
rating: 4,
name: "Mark",
bookId: "0786222727",
},
{
text: "Must read!",
rating: 5,
name: "Raj",
bookId: "0786222727",
},
{
text: "Very expensive",
rating: 3,
name: "Yun",
bookId: "0786222727",
},
{
text: "Extremely satisfied with the storyline!",
rating: 5,
name: "Lisa",
bookId: "0786222727",
}
]);
```
</div>
</TabItem>
</Tabs>
</details>

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

<details>
<summary>Answer</summary>
<div>
```js
db.reviews.deleteMany({"bookId": "0786222727"})
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
const reviews = db.collection("reviews");
await reviews.deleteMany({"bookId": "0786222727"})
```
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.reviews.deleteMany({"bookId": "0786222727"})
```
</div>
</TabItem>
</Tabs>
</details>
33 changes: 25 additions & 8 deletions docs/40-CRUD/5-UPDATE.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# 👐 UPDATE → updateOne(), updateMany()

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

<details>
<summary>Answer</summary>
<div>
```js
db.books.updateOne(
{"title": "Treasure of the Sun"},
{$set: {pages: 449}}
);
```
</div>
<Tabs groupId="challenges" defaultValue={"javascript"}>
<TabItem value="javascript" label="JavaScript">
<div>
```js
await books.updateOne(
{"title": "Treasure of the Sun"},
{$set: {pages: 449}}
);
```
</div>
</TabItem>
<TabItem value="mongosh" label="mongosh">
<div>
```js
db.books.updateOne(
{"title": "Treasure of the Sun"},
{$set: {pages: 449}}
);
```
</div>
</TabItem>
</Tabs>
</details>
Loading