I'm trying to do a search as follows in Mongo:
db.getCollection('years').find({
season: 2018,
}, {
weeks: {
$elemMatch: {
number: 5,
playerStats: {
$elemMatch: {
player: { $in: ['5b7f8e7ac8c0ef318f03ef1c'] }
}
}
}
}
})
Its definitely not returning the correct results, so I'll describe what I'm trying to do:
I have a year document with a field called season of which I want only the season document that matches 2018. Within that there's an array of weeks and I only want the week designated with the number 5. So far so good, I can do this query no problem. Each week document has a playerStats array which I want to filter by their ids, and this is where the problem is, when I run this query, I get one record back with an id and no other fields. I'm new to Mongo so it's possible my design is wrong or my query, any suggestions?
EDIT: Expected result requested:
{
season: 2018,
weeks: [{
number: 5,
playerStats: [...these would be filtered by their player field]
]
}]
}