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:
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user