Let’s say I have docs in the following structure:
[
...,
{
name : 'Adam',
favorites : [
{
id : 1,
name : 'Drinks',
desc : 'Drinks are good',
},
{
id : 3,
name : 'Food',
desc : 'Food is good',
},
{
id : 4,
name : 'Games',
desc : 'Games are good',
},
]
},
]
To reduce query size and reactive re-rendering, I would like to specify the specific fields I need when querying the collection. Ex. if I only needed the name field, I’d do:
col.find({}, { fields : { name : 1 } }).fetch()
I know it is possible to specify fields in embedded arrays. Ex:
col.find({}, { fields : { name: 1, favorites.name : 1 } }).fetch()
This would return:
[
...,
{
name : 'Adam',
favorites : [
{
name : 'Drinks',
},
{
name : 'Food',
},
{
name : 'Games',
},
]
},
]
My problem is as follows: I don’t only want to specify which fields from the embedded array are projected - I also want to specify which array items to project and disregard the rest! Optimally, I would need the method to perform a match as well so it can be dynamic (i.e. I may not be able to easily determine the position(s) of embedded objects, so the solution can’t be to use the positional operator ($) or something similar).
Ex. only project the objects in the favorites
array with id’s [1, 4]. Perhaps the query would look something like this:
col.find({}, { fields : { name: 1, favorites.name: { $match: { id : { $in: [1, 4] } } } } }).fetch()
which would result in excluding the second object in the favorites
array entirely:
[
...,
{
name : 'Adam',
favorites : [
{
name : 'Drinks',
},
{
name : 'Games',
},
]
},
]
That may not be the clearest syntax but regardless, that’s the idea I’m trying to achieve. Any thoughts on how I can do this (+1 if there’s a method to query minimongo in the same way)?
Thanks!