2014-02-25 31 views
0

如果我有一个实体“Email”,它有两个字段:id,address,subject,date。 如果我有一个接口实现MongoRepository:Spring MongoDB返回特殊实体的字段

public interface EMailRepository extends MongoRepository<EMail, String> 
{ 
    public Page<EMail> findByAddressLike(String address,Pageable pageable); 
} 

我想找到的电子邮件只是有ID和地址值,该怎么办呢? 如果我这样做:

@Query(field="{'id','address'}") 
public Page<Email> findByAddressLike(String address,Pageable pageable); 

它会工作吗? 如果不是,你能告诉我如何配置它吗?

回答

0

退房的文档的这一部分: http://docs.spring.io/spring-data/data-mongo/docs/1.4.0.RELEASE/reference/html/mongo.repositories.html#mongodb.repositories.queries.json-based

特别是,这个例子:

public interface PersonRepository extends MongoRepository<Person, String> 

    @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") 
    List<Person> findByThePersonsFirstname(String firstname); 

} 

这很可能你只是需要改变field="{'id', 'address'}"fields="{'address': 1}",我已经离开了ID因为除非明确压制(即{_id: 0}),否则它始终包含在内。