Skip to content

Commit 8e89e7b

Browse files
authored
Merge pull request #39 from mongodb-developer/fixed-code-samples
Fixed data samples to run directly on Atlas UI
2 parents 6b8fb3b + 7a87732 commit 8e89e7b

18 files changed

+894
-175
lines changed

docs/30-simple-queries/1-empty-aggregation.mdx

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ This code is the equivalent of a `SELECT * FROM AUTHORS`. It returns a [cursor](
1313

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

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

2021
```js
2122
[]
2223
```
23-
24+
25+
You're getting all documents in `authors`.
2426
</TabItem>
2527

2628
<TabItem value="mongodb-shell" label="MongoDB Shell">

docs/30-simple-queries/2-match.mdx

+71-39
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import TabItem from '@theme/TabItem';
33

44
# 👐 $match
55

6-
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:
6+
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:
77

88
```js
99
{ $match: { <expression>: <value> } }
1010
```
1111

1212
## Expressions
1313

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

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

4848
```js
49-
db.books.aggregate([{$match: {year: 2010}}])
49+
db.books.aggregate([
50+
{
51+
$match: {year: 2010}
52+
}
53+
])
5054
```
5155
</TabItem>
5256
</Tabs>
@@ -62,16 +66,20 @@ The `<expression>` portion of the $match operator can be any valid MongoDB expre
6266
<TabItem value="atlas" label="Atlas UI">
6367
```js
6468
[
65-
{
66-
$match: { pages: 100 }
67-
}
69+
{
70+
$match: { pages: 100 }
71+
}
6872
]
6973
```
7074

7175
</TabItem>
7276
<TabItem value="mongodb-shell" label="MongoDB Shell">
7377
```js
74-
db.books.aggregate([{$match: {pages: 100}}])
78+
db.books.aggregate([
79+
{
80+
$match: { pages: 100 }
81+
}
82+
])
7583
```
7684
</TabItem>
7785
</Tabs>
@@ -82,44 +90,50 @@ db.books.aggregate([{$match: {pages: 100}}])
8290

8391
## AND
8492

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

87-
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:
95+
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:
8896

8997
<Tabs groupId="aggregations">
9098
<TabItem value="atlas" label="Atlas UI">
9199
```js
92100
[
93-
{
94-
$match: {
95-
$and: [
96-
{ pages: 100 },
97-
{ totalInventory: 2 }
98-
]
99-
}
101+
{
102+
$match: {
103+
$and: [
104+
{ pages: 100 },
105+
{ totalInventory: 1 }
106+
]
100107
}
108+
}
101109
]
102110
```
103111
</TabItem>
104112
<TabItem value="mongodb-shell" label="MongoDB Shell">
105113
```js
106-
db.books.aggregate([{$match: {$and: [{pages: 100}, {totalInventory: 2}]}}])
114+
db.books.aggregate([
115+
{
116+
$match: {
117+
$and: [
118+
{ pages: 100 },
119+
{ totalInventory: 1 }
120+
]
121+
}
122+
}
123+
])
107124
```
108125
</TabItem>
109126
</Tabs>
110127

111-
112-
113-
114128
The pseudo-code for this would be something like:
115129

116130
```
117-
IF pages == 100 AND totalInventory == 2 {
131+
IF pages == 100 AND totalInventory == 1 {
118132
return matching docs
119133
}
120134
```
121135

122-
👐 Return all the `books` from 2015 that have exactly 100 pages.
136+
👐 Return all the `books` from 2009 that have exactly 192 pages.
123137

124138
<details>
125139
<summary>Answer</summary>
@@ -129,28 +143,37 @@ IF pages == 100 AND totalInventory == 2 {
129143
<TabItem value="atlas" label="Atlas UI">
130144
```js
131145
[
132-
{
133-
$match: {
134-
$and: [
135-
{ pages: 100 },
136-
{ year: 2015 }
137-
]
138-
}
146+
{
147+
$match: {
148+
$and: [
149+
{ pages: 192 },
150+
{ year: 2009 }
151+
]
139152
}
153+
}
140154
]
141155
```
142156
</TabItem>
143157
<TabItem value="mongodb-shell" label="MongoDB Shell">
144158
```js
145-
db.books.aggregate([{$match: {$and: [{pages: 100}, {year: 2015}]}}])
159+
db.books.aggregate([
160+
{
161+
$match: {
162+
$and: [
163+
{ pages: 192 },
164+
{ year: 2009 }
165+
]
166+
}
167+
}
168+
])
146169
```
147170
</TabItem>
148171
</Tabs>
149172

150173
</div>
151174
</details>
152175

153-
👐 How many are there?
176+
👐 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" }`
154177

155178
<details>
156179
<summary>Answer</summary>
@@ -176,7 +199,12 @@ db.books.aggregate([{$match: {$and: [{pages: 100}, {year: 2015}]}}])
176199
</TabItem>
177200
<TabItem value="mongodb-shell" label="MongoDB Shell">
178201
```js
179-
db.books.aggregate([{$match: {$and: [{pages: 100}, {year: 2015}]}}]).itcount()
202+
db.books.aggregate(
203+
[
204+
{ $match: {$and: [{pages: 100}, {year: 2015}]} },
205+
{ $count: "books_count" }
206+
]
207+
)
180208
```
181209
</TabItem>
182210
</Tabs>
@@ -191,21 +219,25 @@ We can do an implicit AND just passing a document with all the conditions (inste
191219
<TabItem value="atlas" label="Atlas UI">
192220
```js
193221
[
194-
{
195-
$match: {pages: 100, totalInventory: 2}
196-
}
222+
{
223+
$match: { pages: 100, totalInventory: 2 }
224+
}
197225
]
198226
```
199227
</TabItem>
200228
<TabItem value="mongodb-shell" label="MongoDB Shell">
201229
```js
202-
db.books.aggregate([{$match: {pages: 100, totalInventory: 2}}])
230+
db.books.aggregate([
231+
{
232+
$match: { pages: 100, totalInventory: 2 }
233+
}
234+
])
203235
```
204236
</TabItem>
205237
</Tabs>
206238

207239

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

210242
<details>
211243
<summary>Answer</summary>
@@ -216,14 +248,14 @@ db.books.aggregate([{$match: {pages: 100, totalInventory: 2}}])
216248
```js
217249
[
218250
{
219-
$match: {pages: 100, year: 2015}
251+
$match: {pages: 192, year: 2009}
220252
}
221253
]
222254
```
223255
</TabItem>
224256
<TabItem value="mongodb-shell" label="MongoDB Shell">
225257
```js
226-
db.books.aggregate([{$match: {pages: 100, year: 2015}}])
258+
db.books.aggregate([{$match: {pages: 192, year: 2009}}])
227259
```
228260
</TabItem>
229261
</Tabs>

docs/30-simple-queries/3-project.mdx

+20-8
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,26 @@ If we're interested in the titles, we can use `$project` to select just the fiel
6767
```js
6868
[
6969
{
70-
$project: {title: 1, year: 1}
70+
$project: { title: 1, year: 1 }
7171
}
7272
]
7373
```
7474
</TabItem>
7575
<TabItem value="mongodb-shell" label="MongoDB Shell">
7676

7777
```js
78-
db.books.aggregate([{$project: {title: 1, year: 1}}])
78+
db.books.aggregate([
79+
{
80+
$project: { title: 1, year: 1 }
81+
}
82+
])
7983
```
8084
</TabItem>
8185
</Tabs>
8286

8387

84-
- 1 means "show that field."
85-
- 0 means "hide that field."
88+
- 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)
89+
- 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)
8690
- The primary key `_id` field is shown by default.
8791

8892
So we can exclude fields and show all fields except `attributes` using:
@@ -92,14 +96,18 @@ So we can exclude fields and show all fields except `attributes` using:
9296
```js
9397
[
9498
{
95-
$project: {attributes: 0}
99+
$project: { attributes: 0 }
96100
}
97101
]
98102
```
99103
</TabItem>
100104
<TabItem value="mongodb-shell" label="MongoDB Shell">
101105
```js
102-
db.books.aggregate([{$project: {attributes: 0}}])
106+
db.books.aggregate([
107+
{
108+
$project: { attributes: 0 }
109+
}
110+
])
103111
```
104112
</TabItem>
105113
</Tabs>
@@ -116,14 +124,18 @@ db.books.aggregate([{$project: {attributes: 0}}])
116124
```js
117125
[
118126
{
119-
$project: {title: 1, cover: 1}
127+
$project: { title: 1, cover: 1 }
120128
}
121129
]
122130
```
123131
</TabItem>
124132
<TabItem value="mongodb-shell" label="MongoDB Shell">
125133
```js
126-
db.books.aggregate([{$project: {title: 1, cover: 1}}])
134+
db.books.aggregate([
135+
{
136+
$project: { title: 1, cover: 1 }
137+
}
138+
])
127139
```
128140
</TabItem>
129141
</Tabs>

docs/30-simple-queries/4-limiting-results.mdx

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ If we return too many documents but we're interested in only a few, we can limit
99
<TabItem value="atlas" label="Atlas UI">
1010
```js
1111
[
12-
{ $limit: 1 }
12+
{ $limit: 1 }
1313
]
1414
```
1515
</TabItem>
1616
<TabItem value="mongodb-shell" label="MongoDB Shell">
1717
```js
18-
db.books.aggregate([{$limit: 1}])
18+
db.books.aggregate([
19+
{ $limit: 1 }
20+
])
1921
```
2022
</TabItem>
2123
</Tabs>
@@ -33,13 +35,15 @@ This returns just one document.
3335
<TabItem value="atlas" label="Atlas UI">
3436
```js
3537
[
36-
{ $limit: 7 }
38+
{ $limit: 7 }
3739
]
3840
```
3941
</TabItem>
4042
<TabItem value="mongodb-shell" label="MongoDB Shell">
4143
```js
42-
db.books.aggregate([{$limit: 7}])
44+
db.books.aggregate([
45+
{ $limit: 7 }
46+
])
4347
```
4448
</TabItem>
4549
</Tabs>

0 commit comments

Comments
 (0)