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