I would like to use mongo projections in order to return less data to my application. I would like to know if it's possible.
Example:
user: {
id: 123,
some_list: [{x:1, y:2}, {x:3, y:4}],
other_list: [{x:5, y:2}, {x:3, y:4}]
}
Given a query for user_id = 123
and some 'projection filter' like user.some_list.x = 1
and user.other_list.x = 1
is it possible to achieve the given result?
user: {
id: 123,
some_list: [{x:1, y:2}],
other_list: []
}
The ideia is to make mongo work a little more and retrieve less data to the application. In some cases, we are discarding 80% of the elements of the collections at the application's side. So, it would be better not returning then at all.
Questions:
- Is it possible?
- How can I achieve this. $elemMatch doesn't seem to help me. I'm trying something with unwind, but not getting there
- If it's possible, can this projection filtering benefit from a index on
user.some_list.x
for example? Or not at all once the user was already found by its id?
Thank you.