You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/40-using-arrays/40-search-inside-objects-in-arrays.mdx
+64-5
Original file line number
Diff line number
Diff line change
@@ -131,10 +131,7 @@ db.books.aggregate([
131
131
</div>
132
132
</details>
133
133
134
-
## $unwind
135
-
136
-
This is OK, but we get all attributes, although we're only interested in the MSRP!
137
-
134
+
This is okay, but We get a list of documents with a whole bunch of attributes which looks like the following:
138
135
139
136
```js
140
137
{
@@ -168,7 +165,13 @@ This is OK, but we get all attributes, although we're only interested in the MSR
168
165
]
169
166
}
170
167
```
171
-
To improve, we'll use `$unwind`:
168
+
169
+
What if we are only interested in the MSRP?
170
+
171
+
There is more than one way to filter the unncessary keys and values:
172
+
173
+
### 1. Using $unwind aggregation stage
174
+
See the following pipeline:
172
175
173
176
<TabsgroupId="aggregations">
174
177
<TabItemvalue="atlas"label="Atlas UI">
@@ -225,4 +228,60 @@ db.books.aggregate([
225
228
226
229
You should get one document per attribute of the original book. All fields in these returned documents should be the same, except the ones in attributes.
227
230
231
+
### 🦸 2. Using $arrayToObject operator
232
+
233
+
We can also flatten the `attributes` array into an object using the `$arrayToObject` operator and then use a `$project` stage to filter unwanted fields.
234
+
Note that `$arrayToObject` is not an aggregation stage.
0 commit comments