I am attempting to use Spring to Query a Mongo repository and filter an array subdocument. I have referenced how to filter array in subdocument with mongodb, but was wondering if there was a more appropriate or java structured method to do so using Spring.
I am currently using the shorthand repository interface notation, but I am getting back the complete document with the array not filtered.
PersonRepository.java
@Repository
public interface PersonRepository extends MongoRepository <Person, String> {
List<Person> findByAddressZipCode(@Param("zip") int zip);
}
Person.java
@Document
public class Person {
@Id
private String id;
private String firstName;
private String lastName;
private List<Address> address;
}
Address.java
public class Address {
private int zip;
}
Sample Input
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"98765"
},{
"zip":"12345"
}]
}
Expected Output
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"12345"
}]
}
Actual Output
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"98765"
},{
"zip":"12345"
}]
}