1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-09-02 22:02:39 +02:00

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.
This commit is contained in:
Manoj Patra
2025-05-17 20:44:22 +05:30
committed by GitHub
parent 6e1e334406
commit 00467decca

View File

@@ -37,14 +37,28 @@ If you want to find all the students who have scored 80 or above in Math and 70
```javascript ```javascript
db.courseRecords.find({ db.courseRecords.find({
$and: [
{
grades: { grades: {
$elemMatch: { $elemMatch: {
subject: 'Math', subject: "Math",
score: { $gte: 80 }, score: {
subject: 'English', $gte: 80
score: { $gte: 70 }, }
}, }
}
}, },
{
grades: {
$elemMatch: {
subject: "English",
score: {
$gte: 70
}
}
}
}
]
}); });
``` ```