Reputation: 67
like in question is it possible to create that query?
db.persons.find({ 'oi': '5f2417e3c655cb13e85186df', 'ch': { $elemMatch: { 'type': { $in: ['MAN'] }}}}, {'ch.$': 1})
The last part of this query is problematic. How to retrive fields that pass the $elemMatch predicate only.
Spring data @Query annotation have field property but if specify it by {'children: 1'}
I retrive all children instead of this which pass the query.
{ 'children.$': 1}
doesn't work of course.
Upvotes: 0
Views: 223
Reputation: 1382
You can use @Query
annotation. Pass the query in value and projection in fields inside @Query
annotation like this
@Query(value = "{ 'oi': ?0, 'ch': { $elemMatch: { 'type': { $in: ?1 }}}}", fields = "{'ch.$' : 1}")
Person findPersonCustom(String id, List<String> types);
Here ?0
& ?1
will be passed as method arguments 0 & 1 respectively. You can also put that values inside @Query
directly if they are static.
The resulted Person
object will contain only matched elements from ch
array.
Upvotes: 1