From 00467decca560f97b308782df736647716d39b01 Mon Sep 17 00:00:00 2001 From: Manoj Patra <30291410+manojpatra061@users.noreply.github.com> Date: Sat, 17 May 2025 20:44:22 +0530 Subject: [PATCH] fix(elem-match.md): correct $elemMatch usage in example (#8662) The original example attempts to match multiple 'subject' and 'score' values within a single $elemMatch, which is logically incorrect. Due to key overwriting, only the last 'subject' and 'score' were matched. Updated the query to use $and with separate $elemMatch conditions for "Math" and "English" subjects. --- .../array-operators/elem-match.md | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/data/roadmaps/mongodb/content/query-operators/array-operators/elem-match.md b/src/data/roadmaps/mongodb/content/query-operators/array-operators/elem-match.md index 19edc0630..bda2ccf76 100644 --- a/src/data/roadmaps/mongodb/content/query-operators/array-operators/elem-match.md +++ b/src/data/roadmaps/mongodb/content/query-operators/array-operators/elem-match.md @@ -37,14 +37,28 @@ If you want to find all the students who have scored 80 or above in Math and 70 ```javascript db.courseRecords.find({ - grades: { - $elemMatch: { - subject: 'Math', - score: { $gte: 80 }, - subject: 'English', - score: { $gte: 70 }, + $and: [ + { + grades: { + $elemMatch: { + subject: "Math", + score: { + $gte: 80 + } + } + } }, - }, + { + grades: { + $elemMatch: { + subject: "English", + score: { + $gte: 70 + } + } + } + } + ] }); ```