1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-08-30 20:49:49 +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
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
}
}
}
}
]
});
```