Skip to content

Commit 7d38e3c

Browse files
committed
Fixes in arrays and count
1 parent 0e55d14 commit 7d38e3c

File tree

5 files changed

+432
-139
lines changed

5 files changed

+432
-139
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import Tabs from '@theme/Tabs';
2+
import TabItem from '@theme/TabItem';
3+
4+
# 👐 Types of arrays
5+
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`.
7+
8+
Let's get one book:
9+
10+
11+
<Tabs groupId="aggregations">
12+
<TabItem value="atlas" label="Atlas UI">
13+
14+
Remember to select the `books` collection in the UI.
15+
16+
```js
17+
[
18+
{ $limit: 1 }
19+
]
20+
```
21+
</TabItem>
22+
23+
<TabItem value="mongodb-shell" label="MongoDB Shell">
24+
25+
```js
26+
db.books.aggregate([
27+
{ $limit: 1 }
28+
])
29+
```
30+
</TabItem>
31+
</Tabs>
32+
33+
👐 Run this aggregation to get one book.
34+
35+
I got this one. (It can change depending on the data source you imported.)
36+
37+
```js
38+
[
39+
{
40+
"_id": "0002005018",
41+
"title": "Clara Callan: A novel",
42+
"authors": [
43+
{
44+
"_id": "64cc2db4830ba29148da4c3b",
45+
"name": "Richard Bruce Wright"
46+
}
47+
],
48+
"genres": [
49+
"Women Teachers",
50+
"Young Women",
51+
"Actresses",
52+
"Sisters"
53+
],
54+
"pages": 414,
55+
"year": 2001,
56+
"synopsis": "Giller Prize Winner 2001. Richard B. Wright. A Phyllis Bruce Book.",
57+
"cover": "https://images.isbndb.com/covers/50/12/9780002005012.jpg",
58+
"attributes": [
59+
{
60+
"key": "edition",
61+
"value": "1st"
62+
},
63+
{
64+
"key": "dimensions",
65+
"value": "Height: 11.11 Inches, Length: 6.11 Inches, Weight: 1 Pounds, Width: 1.11 Inches"
66+
},
67+
{
68+
"key": "isbn13",
69+
"value": "9780002005012"
70+
},
71+
{
72+
"key": "msrp",
73+
"value": "0.00"
74+
},
75+
{
76+
"key": "isbn",
77+
"value": "0002005018"
78+
},
79+
{
80+
"key": "isbn10",
81+
"value": "0002005018"
82+
}
83+
],
84+
"totalInventory": 2,
85+
"available": 2,
86+
"binding": "Hardcover",
87+
"language": "en",
88+
"publisher": "HarperFlamingoCanada",
89+
"longTitle": "Clara Callan: A novel",
90+
"reviews": [
91+
{
92+
"_id": {
93+
"$oid": "6526bbc2e4e804888dfedf37"
94+
},
95+
"text": "yd",
96+
"name": "Holy Dingo",
97+
"rating": 1,
98+
"timestamp": 1697037250625
99+
},
100+
{
101+
"_id": {
102+
"$oid": "651c0f4b24193d51c4c734a3"
103+
},
104+
"text": "Great!",
105+
"name": "Brash Platypus",
106+
"rating": 5,
107+
"timestamp": 1696337739128
108+
}
109+
]
110+
}
111+
]
112+
```
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import Tabs from '@theme/Tabs';
2+
import TabItem from '@theme/TabItem';
3+
4+
# 👐 Scalar arrays
5+
6+
## 👐 Get all the Science Fiction Books
7+
8+
Can I get all books for genre `Science Fiction`?. Turns out it's quite simple:
9+
10+
<Tabs groupId="aggregations">
11+
<TabItem value="atlas" label="Atlas UI">
12+
13+
```js
14+
[
15+
{
16+
$match: {
17+
genres: 'Science Fiction'
18+
}
19+
}
20+
]
21+
```
22+
23+
</TabItem>
24+
25+
<TabItem value="mongodb-shell" label="MongoDB Shell">
26+
```js
27+
db.books.aggregate([
28+
{
29+
$match: {
30+
genres: 'Science Fiction'
31+
}
32+
}
33+
])
34+
```
35+
36+
</TabItem>
37+
</Tabs>
38+
39+
Remember that will include any book that has any other genre as long as it has `Science Fiction` in `genres`.
40+
41+
## 👐 Find all the books that belong only to the genres "Fiction" and "Science Fiction"
42+
43+
In this case, we want books that have both "Fiction" and "Science Fiction" in the genres array _and nothing else_.
44+
45+
<Tabs groupId="aggregations">
46+
<TabItem value="atlas" label="Atlas UI">
47+
48+
```js
49+
[
50+
{
51+
$match: {
52+
genres: ['Fiction', 'Science Fiction']
53+
}
54+
},
55+
{$project: {
56+
title: 1,
57+
genres: 1
58+
}}
59+
]
60+
```
61+
62+
</TabItem>
63+
64+
<TabItem value="mongodb-shell" label="MongoDB Shell">
65+
```js
66+
db.books.aggregate([
67+
{
68+
$match: {
69+
genres: ['Fiction', 'Science Fiction']
70+
}
71+
},
72+
{$project: {
73+
title: 1,
74+
genres: 1
75+
}}
76+
])
77+
```
78+
79+
</TabItem>
80+
</Tabs>
81+
82+
Here we're comparing the whole array. __Elements order is important__. With this comparison you'll get nothing in return:
83+
84+
<Tabs groupId="aggregations">
85+
<TabItem value="atlas" label="Atlas UI">
86+
87+
```js
88+
[
89+
{
90+
$match: {
91+
genres: ['Science Fiction', 'Fiction']
92+
}
93+
},
94+
{$project: {
95+
title: 1,
96+
genres: 1
97+
}}
98+
]
99+
```
100+
101+
</TabItem>
102+
103+
<TabItem value="mongodb-shell" label="MongoDB Shell">
104+
```js
105+
db.books.aggregate([
106+
{
107+
$match: {
108+
genres: ['Science Fiction', 'Fiction']
109+
}
110+
},
111+
{$project: {
112+
title: 1,
113+
genres: 1
114+
}}
115+
])
116+
```
117+
118+
</TabItem>
119+
</Tabs>
120+
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:
125+
126+
<Tabs groupId="aggregations">
127+
<TabItem value="atlas" label="Atlas UI">
128+
129+
```js
130+
[
131+
{
132+
$match: {
133+
genres: {$all: ['Science Fiction', 'Fiction'] }
134+
}
135+
},
136+
{$project: {
137+
title: 1,
138+
genres: 1
139+
}}
140+
]
141+
```
142+
143+
</TabItem>
144+
145+
<TabItem value="mongodb-shell" label="MongoDB Shell">
146+
```js
147+
db.books.aggregate([
148+
{
149+
$match: {
150+
genres: {$all: ['Science Fiction', 'Fiction'] }
151+
}
152+
},
153+
{$project: {
154+
title: 1,
155+
genres: 1
156+
}}
157+
])
158+
```
159+
160+
</TabItem>
161+
</Tabs>

0 commit comments

Comments
 (0)