I have the below query. What I am trying to do is always populate sets but only populate stats if they exist for that user.
Currently only the sets that have a stats collection are being returned. What I want is all sets and then populate stats for that user.
My schema is basically the same for deck, set, and card in that they all have a collection of stats where one of those stats belong to a specific user. The catch is that a user may not have a stats for that entity.
My results:
{
deck: {
user: 1,
stats: [{ id: 1 }, { id: 2 }],
sets: [{id:1, stats: [{id:1, user: 1}]}],
...
}
}
What I expect:
{
deck: {
user: 1,
stats: [{ id: 1 }, { id: 2 }],
sets: [{id:1, stats: [{id:1, user: 1}]}, {id:2, stats: []}],
...
}
}
Query:
const deck = await req.entityManager.findOne(
Deck,
{
user,
id: deckId,
},
{
populate: ['stats', 'sets.stats', 'sets.cards.stats'],
populateWhere: {
stats: { user },
sets: {
$or: [{ stats: { user } }, { stats: [] }],
cards: { stats: { user } },
},
},
},
);