From c7dfd8dc13e07b91e40a1dfd17801e8dd5939378 Mon Sep 17 00:00:00 2001 From: Wenjie Teo Date: Tue, 23 Apr 2024 22:13:02 +0700 Subject: [PATCH 1/2] update array match update direct matching of documents in an array actually works, so we need to re-word the messaging in this document --- .../2-search-inside-objects-in-arrays.mdx | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx b/docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx index 8e23ac5..b4b2ea6 100644 --- a/docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx +++ b/docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx @@ -31,19 +31,7 @@ attributes: [ ], ``` -How do we search for all the books that have an msrp of 9.99? We want books that, inside `attributes` has an object with key `msrp` and value `9.99`. We can think this works: - -```js -let nineNinetyNine = {$match: {"attributes.key": "msrp", "attributes.value": 9.99}}; -let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1, "attributes": 1}}; - -db.books.aggregate([ - nineNinetyNine, - showOnlyTheseFields, -]); -``` - -But it doesn't. This one works: +How do we search for all the books that have an msrp of 9.99? We want books that, inside `attributes` has an object with key `msrp` and value `9.99`. We can get it to work with this: ```js db.books.aggregate([ @@ -63,5 +51,18 @@ db.books.aggregate([ { $unwind : "$attributes" }, ]); ``` +You should get one document per attribute of the original book. + +But you can actually match documents in an array in a much more straightforward way: + +```js +let nineNinetyNine = {$match: {"attributes.key": "msrp", "attributes.value": 9.99}}; +let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1, "attributes": 1}}; + +db.books.aggregate([ + nineNinetyNine, + showOnlyTheseFields, +]); +``` + -You should get one document per attribute of the original book. \ No newline at end of file From 953f4cde3d51221a2d2298ea20f1cb7e13988ee0 Mon Sep 17 00:00:00 2001 From: Wenjie Teo Date: Tue, 23 Apr 2024 22:20:47 +0700 Subject: [PATCH 2/2] Update 2-search-inside-objects-in-arrays.mdx --- docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx b/docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx index b4b2ea6..b49052a 100644 --- a/docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx +++ b/docs/40-using-arrays/2-search-inside-objects-in-arrays.mdx @@ -53,7 +53,7 @@ db.books.aggregate([ ``` You should get one document per attribute of the original book. -But you can actually match documents in an array in a much more straightforward way: +But you can actually match documents in an array in a more straightforward fashion: ```js let nineNinetyNine = {$match: {"attributes.key": "msrp", "attributes.value": 9.99}};