MongoDB Spring Data Projection仅返回第一个匹配元素

时间:2016-05-06 01:41:02

标签: java mongodb spring-data-mongodb mongorepository

我有

@Document
public class Employee
{
 @Id
 Long empCode;
 String empSurname;
 String address;
// getters setters
}

然后我有MongoRepository的扩展

public interface EmployeeRepository extends MongoRepository<Employee, Long>
{
  EmployeeProject findAddressByEmpSurname(String surName);
}

EmployeeProject是一个投影

public interface EmployeeProject 
{
    public List<String> getAddress();
}

我正在打电话

EmployeeProject param = employeeRepository.findAddressByEmpSurname("Smith");
param.getAddress().size(); -> Returns the first matched element only

我需要getAddress()将所有匹配的值作为String返回,而不仅仅是第一个。

非常感谢任何帮助。谢谢。 PS:如果我直接在MongoDB数据库上运行生成的查询,我会收回所有文档。 看来我需要一个Collection Projection,但我似乎无法使语法正确。

0 个答案:

没有答案
相关问题