I am using spring-data-mongodb 1.9.1.
My question is pretty similar to this one.
I have
@Document
public class Employee{
private int userId;
private List<Address> addresses;
}
And
@Document
public class Address{
private int addressId;
private String street;
private String city:
}
// Getters and setters
I have written a query and can get the value I need returned as an Employee object.
@Query(value="{ '$and' : [ {'addresses.city' : ?0 }]} ", fields="{ 'addresses.street' : 1}")
List<Employee> findStreetByEmployeeId(String fieldName);
My question is how can I get
List<String> findStreetByEmployeeId(String fieldName);
In other words, I want to just extract the part of the result in which I am interested. If I have to loop through all Employee objects and extract the populated street value, it can become a very expensive operation.
Any thoughts on how to achieve this would be much appreciated. Thanks.