Skip to content

Commit

Permalink
Merge pull request #39 from mongodb-developer/fixed-code-samples
Browse files Browse the repository at this point in the history
Fixed data samples to run directly on Atlas UI
  • Loading branch information
dfreniche authored Sep 19, 2024
2 parents 6b8fb3b + 7a87732 commit 8e89e7b
Show file tree
Hide file tree
Showing 18 changed files with 894 additions and 175 deletions.
4 changes: 3 additions & 1 deletion docs/30-simple-queries/1-empty-aggregation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ This code is the equivalent of a `SELECT * FROM AUTHORS`. It returns a [cursor](

<Screenshot src="/img/30-simple-queries/atlas-aggregation.png" url="http://cloud.mongodb.com/" alt="Atlas UI showing an empty aggregation pipeline" />

- Make sure `Collections` > `authors` is selected.
- Open the `Aggregation` tab.
- Select `Text`.
- Notice the empty array in the editor denoting an empty aggregation pipeline:

```js
[]
```


You're getting all documents in `authors`.
</TabItem>

<TabItem value="mongodb-shell" label="MongoDB Shell">
Expand Down
110 changes: 71 additions & 39 deletions docs/30-simple-queries/2-match.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import TabItem from '@theme/TabItem';

# 👐 $match

The $match operator is used in conjunction with the aggregation framework to filter documents in a collection. It takes a document as input and returns a new document containing only the documents that match the specified criteria. The syntax for the $match operator is as follows:
The [$match](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/) stage filters documents in a collection. It takes a document as input (your filter) and returns the documents that match the specified criteria. The syntax for the $match stage is as follows:

```js
{ $match: { <expression>: <value> } }
```

## Expressions

The `<expression>` portion of the $match operator can be any valid MongoDB expression. This includes:
The `<expression>` portion of the $match stage can be any valid MongoDB expression. This includes:

* Comparison operators: `eq`, `neq`, `gte`, `lte`, `gt`, `lt`, `in`, `nin`, `exists`.
* Regular expressions: regex.
Expand Down Expand Up @@ -46,7 +46,11 @@ The `<expression>` portion of the $match operator can be any valid MongoDB expre
Say we want all the books from 2010. We'll write:

```js
db.books.aggregate([{$match: {year: 2010}}])
db.books.aggregate([
{
$match: {year: 2010}
}
])
```
</TabItem>
</Tabs>
Expand All @@ -62,16 +66,20 @@ The `<expression>` portion of the $match operator can be any valid MongoDB expre
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: { pages: 100 }
}
{
$match: { pages: 100 }
}
]
```

</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {pages: 100}}])
db.books.aggregate([
{
$match: { pages: 100 }
}
])
```
</TabItem>
</Tabs>
Expand All @@ -82,44 +90,50 @@ db.books.aggregate([{$match: {pages: 100}}])

## AND

If we need to add more conditions using AND, we can do it with the `$and` operator.
If we need to add more conditions using AND, we use the `$and` operator.

If we want all the books with 100 pages with exactly `totalInventory` 2, we can use a `$and` operator. This takes an array of documents with all the conditions that should be true for the AND to succeed:
If we want all the books with 100 pages and with exactly `totalInventory` 1, we can use the `$and` operator. It takes an array of documents with all the conditions that should be true for the AND to succeed:

<Tabs groupId="aggregations">
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: {
$and: [
{ pages: 100 },
{ totalInventory: 2 }
]
}
{
$match: {
$and: [
{ pages: 100 },
{ totalInventory: 1 }
]
}
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {$and: [{pages: 100}, {totalInventory: 2}]}}])
db.books.aggregate([
{
$match: {
$and: [
{ pages: 100 },
{ totalInventory: 1 }
]
}
}
])
```
</TabItem>
</Tabs>




The pseudo-code for this would be something like:

```
IF pages == 100 AND totalInventory == 2 {
IF pages == 100 AND totalInventory == 1 {
return matching docs
}
```

👐 Return all the `books` from 2015 that have exactly 100 pages.
👐 Return all the `books` from 2009 that have exactly 192 pages.

<details>
<summary>Answer</summary>
Expand All @@ -129,28 +143,37 @@ IF pages == 100 AND totalInventory == 2 {
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: {
$and: [
{ pages: 100 },
{ year: 2015 }
]
}
{
$match: {
$and: [
{ pages: 192 },
{ year: 2009 }
]
}
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {$and: [{pages: 100}, {year: 2015}]}}])
db.books.aggregate([
{
$match: {
$and: [
{ pages: 192 },
{ year: 2009 }
]
}
}
])
```
</TabItem>
</Tabs>

</div>
</details>

👐 How many are there?
👐 How many are there? We haven't yet seen the `$count` stage, but try to adding a second stage to your pipeline with `{ $count: "books_count" }`

<details>
<summary>Answer</summary>
Expand All @@ -176,7 +199,12 @@ db.books.aggregate([{$match: {$and: [{pages: 100}, {year: 2015}]}}])
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {$and: [{pages: 100}, {year: 2015}]}}]).itcount()
db.books.aggregate(
[
{ $match: {$and: [{pages: 100}, {year: 2015}]} },
{ $count: "books_count" }
]
)
```
</TabItem>
</Tabs>
Expand All @@ -191,21 +219,25 @@ We can do an implicit AND just passing a document with all the conditions (inste
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: {pages: 100, totalInventory: 2}
}
{
$match: { pages: 100, totalInventory: 2 }
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {pages: 100, totalInventory: 2}}])
db.books.aggregate([
{
$match: { pages: 100, totalInventory: 2 }
}
])
```
</TabItem>
</Tabs>


👐 Return all the `books` from 2015 that have exactly 100 pages, using the simple $and notation:
👐 Return all the `books` from 2009 that have exactly 192 pages, using the shorthand `$and` notation:

<details>
<summary>Answer</summary>
Expand All @@ -216,14 +248,14 @@ db.books.aggregate([{$match: {pages: 100, totalInventory: 2}}])
```js
[
{
$match: {pages: 100, year: 2015}
$match: {pages: 192, year: 2009}
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {pages: 100, year: 2015}}])
db.books.aggregate([{$match: {pages: 192, year: 2009}}])
```
</TabItem>
</Tabs>
Expand Down
28 changes: 20 additions & 8 deletions docs/30-simple-queries/3-project.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,26 @@ If we're interested in the titles, we can use `$project` to select just the fiel
```js
[
{
$project: {title: 1, year: 1}
$project: { title: 1, year: 1 }
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">

```js
db.books.aggregate([{$project: {title: 1, year: 1}}])
db.books.aggregate([
{
$project: { title: 1, year: 1 }
}
])
```
</TabItem>
</Tabs>


- 1 means "show that field."
- 0 means "hide that field."
- 1 means "show that field.". Once you started an inclusion projection you can't exclude other fields (you just keep adding the fields you want to see)
- 0 means "hide that field.". Once you started an exclusion projection you can't include other fields (you just keep adding the fields you don't want to see)
- The primary key `_id` field is shown by default.

So we can exclude fields and show all fields except `attributes` using:
Expand All @@ -92,14 +96,18 @@ So we can exclude fields and show all fields except `attributes` using:
```js
[
{
$project: {attributes: 0}
$project: { attributes: 0 }
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$project: {attributes: 0}}])
db.books.aggregate([
{
$project: { attributes: 0 }
}
])
```
</TabItem>
</Tabs>
Expand All @@ -116,14 +124,18 @@ db.books.aggregate([{$project: {attributes: 0}}])
```js
[
{
$project: {title: 1, cover: 1}
$project: { title: 1, cover: 1 }
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$project: {title: 1, cover: 1}}])
db.books.aggregate([
{
$project: { title: 1, cover: 1 }
}
])
```
</TabItem>
</Tabs>
Expand Down
12 changes: 8 additions & 4 deletions docs/30-simple-queries/4-limiting-results.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ If we return too many documents but we're interested in only a few, we can limit
<TabItem value="atlas" label="Atlas UI">
```js
[
{ $limit: 1 }
{ $limit: 1 }
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$limit: 1}])
db.books.aggregate([
{ $limit: 1 }
])
```
</TabItem>
</Tabs>
Expand All @@ -33,13 +35,15 @@ This returns just one document.
<TabItem value="atlas" label="Atlas UI">
```js
[
{ $limit: 7 }
{ $limit: 7 }
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$limit: 7}])
db.books.aggregate([
{ $limit: 7 }
])
```
</TabItem>
</Tabs>
Expand Down
Loading

0 comments on commit 8e89e7b

Please sign in to comment.