Mongodb从DBRef中选择一些字段

时间:2015-10-11 14:26:31

标签: spring mongodb mongodb-query spring-data-mongodb

我正在使用Spring 4.1,Spring数据mongodb 1.7.2和MOngodb 3.0构建应用程序。我有以下文档结构。我想在查询答案的同时从零售商处选择一些字段而不是所有字段。如何实现?

我尝试了以下代码,但它没有工作

public List<Answer> findAnswerByLastModifiedAndCreatedBy(Date createDate,String createdBy){

        Query query = Query.query(Criteria.where("lastModifiedDate").gte(createDate).and("answerText").exists(true).ne("").and("createdBy").is(createdBy));

        query.fields().include("answerText").include("id").include("retailer.$shopName").include("retailer.$id");

        List<Answer> answers = mongoTemplate.find(query, Answer.class);
        return answers;
    }

@Document(collection="answer")
@TypeAlias("answer")
public class Answer extends CommonDomainAttributes implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 4195056255932348897L;

    /**
     * 
     */
    public Answer() {
        // TODO Auto-generated constructor stub
    }

    Question question;

    @DBRef
    @Indexed(background=true,name="ANSWER_RETAILER",dropDups=true)
    private Retailer retailer;

    @TextIndexed()
    @Indexed(background=true,name="ANSWER_ANSWRTEXT",dropDups=true)
    private String answerText;
 }

 @Document(collection="retailer")
@TypeAlias("retailer")
public class Retailer extends CommonDomainAttributes implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2712025356211775960L;

    public Retailer() {
        // TODO Auto-generated constructor stub
    }

    @DBRef
    @Indexed(background=true,name="RETAILER_USER",dropDups=true)
    private IdeaRealtyUser user;

    @DBRef(lazy=true)
    private List<Product> products;

    private String shopName;
}

0 个答案:

没有答案