Skip to content

Commit 7ce55f4

Browse files
committed
Added new array instructions based on slides
1 parent f70e0b5 commit 7ce55f4

File tree

3 files changed

+210
-82
lines changed

3 files changed

+210
-82
lines changed

docs/40-using-arrays/10-types-of-arrays.mdx

+43-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import TabItem from '@theme/TabItem';
33

44
# 👐 Types of arrays
55

6-
A JSON array can contain __scalar values__ or __objects__. In our data, `books` have a scalar array of the `genres` this book belongs to. It also has several arrays of objects, like the `authors` of a book, `attributes` and `reviews`.
6+
A JSON array can contain __simple values__ (scalar values) or __objects__. In our data, `books` have a scalar array of the `genres` this book belongs to. It also has several arrays of objects, like the `authors` of a book, `attributes` and `reviews`.
77

88
Let's get one book:
99

@@ -110,3 +110,45 @@ I got this one. (It can change depending on the data source you imported.)
110110
}
111111
]
112112
```
113+
114+
### Array of strings example
115+
116+
```json
117+
"genres": [
118+
"Women Teachers",
119+
"Young Women",
120+
"Actresses",
121+
"Sisters"
122+
],
123+
```
124+
125+
### Array of objects example
126+
127+
```json
128+
"attributes": [
129+
{
130+
"key": "edition",
131+
"value": "1st"
132+
},
133+
{
134+
"key": "dimensions",
135+
"value": "Height: 11.11 Inches, Length: 6.11 Inches, Weight: 1 Pounds, Width: 1.11 Inches"
136+
},
137+
{
138+
"key": "isbn13",
139+
"value": "9780002005012"
140+
},
141+
{
142+
"key": "msrp",
143+
"value": "0.00"
144+
},
145+
{
146+
"key": "isbn",
147+
"value": "0002005018"
148+
},
149+
{
150+
"key": "isbn10",
151+
"value": "0002005018"
152+
}
153+
],
154+
```

docs/40-using-arrays/20-simple-match-array.mdx

+61-40
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Tabs from '@theme/Tabs';
22
import TabItem from '@theme/TabItem';
33

4-
# 👐 Scalar arrays
4+
# 👐 Simple arrays
55

66
## 👐 Get all the Science Fiction Books
77

8-
Can I get all books for genre `Science Fiction`?. Turns out it's quite simple:
8+
Can I get all books for the genre `Science Fiction`?. Turns out it's quite simple:
99

1010
<Tabs groupId="aggregations">
1111
<TabItem value="atlas" label="Atlas UI">
@@ -38,23 +38,22 @@ db.books.aggregate([
3838

3939
Remember that will include any book that has any other genre as long as it has `Science Fiction` in `genres`.
4040

41-
## 👐 Find all the books that belong only to the genres "Fiction" and "Science Fiction"
41+
## 👐 Find all the books that belong at least to both genres "Fiction" and "Science Fiction"
4242

43-
In this case, we want books that have both "Fiction" and "Science Fiction" in the genres array _and nothing else_.
43+
If you want to search for all books that have "Fiction" and "Science Fiction", in any order (and possibly other genres) use:
4444

4545
<Tabs groupId="aggregations">
4646
<TabItem value="atlas" label="Atlas UI">
4747

4848
```js
4949
[
50-
{
51-
$match: {
52-
genres: ['Fiction', 'Science Fiction']
53-
}
50+
{$match: {
51+
genres: {$all: ['Science Fiction', 'Fiction'] }
52+
}
5453
},
5554
{$project: {
56-
title: 1,
57-
genres: 1
55+
title: 1,
56+
genres: 1
5857
}}
5958
]
6059
```
@@ -64,31 +63,60 @@ In this case, we want books that have both "Fiction" and "Science Fiction" in th
6463
<TabItem value="mongodb-shell" label="MongoDB Shell">
6564
```js
6665
db.books.aggregate([
67-
{
68-
$match: {
69-
genres: ['Fiction', 'Science Fiction']
70-
}
66+
{$match: {
67+
genres: {$all: ['Science Fiction', 'Fiction'] }
68+
}
7169
},
7270
{$project: {
73-
title: 1,
74-
genres: 1
71+
title: 1,
72+
genres: 1
7573
}}
7674
])
7775
```
7876

7977
</TabItem>
8078
</Tabs>
8179

82-
Here we're comparing the whole array. __Elements order is important__. With this comparison you'll get nothing in return:
80+
## 👐 Find books with either genre
81+
82+
<Tabs groupId="aggregations">
83+
<TabItem value="atlas" label="Atlas UI">
84+
85+
```js
86+
[{
87+
$match: {
88+
genres: { $in: ['Science Fiction', 'Fiction'] }
89+
}
90+
}
91+
]
92+
```
93+
94+
</TabItem>
95+
96+
<TabItem value="mongodb-shell" label="MongoDB Shell">
97+
```js
98+
db.books.aggregate([{
99+
$match: {
100+
genres: { $in: ['Science Fiction', 'Fiction'] }
101+
}
102+
}
103+
]);
104+
```
105+
106+
</TabItem>
107+
</Tabs>
108+
109+
## 👐 Find all the books that belong only to the genres "Fiction" and "Science Fiction"
110+
111+
In this case, we want books that have both "Fiction" and "Science Fiction" in the genres array _and nothing else_. So we're looking for documents that contain a `genres` array exactly as `['Fiction', 'Science Fiction']`. We're not looking for the individual genres that could be inside the array, instead we are comparing the whole array on each document.
83112

84113
<Tabs groupId="aggregations">
85114
<TabItem value="atlas" label="Atlas UI">
86115

87116
```js
88117
[
89-
{
90-
$match: {
91-
genres: ['Science Fiction', 'Fiction']
118+
{$match: {
119+
genres: ['Fiction', 'Science Fiction']
92120
}
93121
},
94122
{$project: {
@@ -103,9 +131,8 @@ Here we're comparing the whole array. __Elements order is important__. With this
103131
<TabItem value="mongodb-shell" label="MongoDB Shell">
104132
```js
105133
db.books.aggregate([
106-
{
107-
$match: {
108-
genres: ['Science Fiction', 'Fiction']
134+
{$match: {
135+
genres: ['Fiction', 'Science Fiction']
109136
}
110137
},
111138
{$project: {
@@ -118,24 +145,20 @@ db.books.aggregate([
118145
</TabItem>
119146
</Tabs>
120147

121-
122-
## 👐 Find all the books that belong at least to the genres "Fiction" and "Science Fiction"
123-
124-
If you want to search for all books that have "Fiction" and "Science Fiction", in any order (and possibly other genres) use:
148+
Here we're comparing the whole array. __Element order is important__. With this comparison you'll get nothing in return:
125149

126150
<Tabs groupId="aggregations">
127151
<TabItem value="atlas" label="Atlas UI">
128152

129153
```js
130154
[
131-
{
132-
$match: {
133-
genres: {$all: ['Science Fiction', 'Fiction'] }
134-
}
155+
{$match: {
156+
genres: ['Science Fiction', 'Fiction']
157+
}
135158
},
136159
{$project: {
137-
title: 1,
138-
genres: 1
160+
title: 1,
161+
genres: 1
139162
}}
140163
]
141164
```
@@ -145,17 +168,15 @@ If you want to search for all books that have "Fiction" and "Science Fiction", i
145168
<TabItem value="mongodb-shell" label="MongoDB Shell">
146169
```js
147170
db.books.aggregate([
148-
{
149-
$match: {
150-
genres: {$all: ['Science Fiction', 'Fiction'] }
151-
}
171+
{$match: {
172+
genres: ['Science Fiction', 'Fiction']
173+
}
152174
},
153175
{$project: {
154-
title: 1,
155-
genres: 1
176+
title: 1,
177+
genres: 1
156178
}}
157179
])
158180
```
159-
160181
</TabItem>
161182
</Tabs>

0 commit comments

Comments
 (0)