I have the following types in typescript.
type Student = {
id: string;
name: string;
score: number;
// ...
}
type Class = {
name: string;
// key = student.id
students: Record<string, Student>
}
For example,
const mathClass: Class = {
name: "Math Class",
students: {
"S01": {
id: "S01",
name: "Alice",
score: 88
},
"S02": {
id: "S02",
name: "Bob",
score: 79
}
}
}
and I insert this into MongoDB.
When I need to perform filtering, is it possible to make a query to select students with score higher than 80? Something like
db.classes.find({"students.*.score": {$gte: 80}})
I prefer not to use aggregation ($objectToArray
) unless that’s the only way.