spring data mongodb embeded query

时间:2018-01-09 15:03:23

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

{
"_id" : "7959305563",
"_class" : "com.loylty.messagingEngine.entities.message.GroupIdDeliveryStatusReport",
"DATA_LIST" : [ 
    {
        "_id" : "7959305562-1",
        "mobile" : "9566337867",
        "status" : "DELIVRD",
        "senttime" : "2018-01-09 14:19:42",
        "dlrtime" : "2018-01-09 14:57:06",
        "custom" : "9566337867"
    }, 
    {
        "_id" : "7959305562-2",
        "mobile" : "9566337867",
        "status" : "DELIVRD",
        "senttime" : "2018-01-09 14:19:42",
        "dlrtime" : "2018-01-09 14:57:05",
        "custom" : "9566337867"
    }, 
    {
        "_id" : "7959305562-3",
        "mobile" : "9566337867",
        "status" : "DELIVRD",
        "senttime" : "2018-01-09 14:19:42",
        "dlrtime" : "2018-01-09 14:57:04",
        "custom" : "9566337867"
    }
 ]
}

GroupIdDeliveryStatusReport的结构

@Document(collection = "GROUP_ID_DELIVERY_STATUS")
public class GroupIdDeliveryStatusReport {

@Id
@Field("GROUP_ID")
private String groupId;

@Field("DATA_LIST")
private List<SolInfiniGroupIdData> data;

public String getGroupId() {
    return groupId;
}

public void setGroupId(String groupId) {
    this.groupId = groupId;
}

public List<SolInfiniGroupIdData> getData() {
    return data;
}

public void setData(List<SolInfiniGroupIdData> data) {
    this.data = data;
}

}

请原谅我如果我无法正确描述。

这是我的集合名称GroupIdDeliveryStatusReport

的结构

我想在此集合中查询并获取状态为 DELIVRD

的人员(DATA_LIST)中的列表计数

对于给定的结构,计数应为3。

我应该如何在spring数据mongodb条件查询中执行此操作?

我编写了shell查询,该查询提供了正确的计数,但仍坚持转换为条件

db.getCollection('GROUP_ID_DELIVERY_STATUS').aggregate(
    { "$unwind" : "$DATA_LIST"},
    {
        "$match" : {"DATA_LIST.status": "DELIVRD" }
    },
    { $group: { _id: null, count: { $sum: 1 } } }
)

这就是我写的方式

package com.loylty.messagingEngine.service.Impl;

import com.loylty.messagingEngine.service.GroupIdDeliveryStatusReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

@Service
public class GroupIdDeliveryStatusReportServiceImpl implements 
GroupIdDeliveryStatusReportService {

@Autowired
private MongoOperations mongoOperations;

@Override
public void getDeliveryCount() {
    Aggregation aggregation = newAggregation(
            //match(Criteria.where("_id").in("7959305563")), //Use this if you want to do this operation for particular document only
            unwind("DATA_LIST"),
            match(Criteria.where("DATA_LIST.status").is("DELIVRD")),
            project("DATA_LIST._id")
    );
    AggregationResults<String> groupResults
            = mongoOperations.aggregate(aggregation, "GroupIdDeliveryStatusReport", String.class);
    int count = groupResults.getMappedResults().size();

}
}

1 个答案:

答案 0 :(得分:0)

这应该有用。

Aggregation aggregation = newAggregation(
        //match(Criteria.where("_id").in("7959305563")), Use this if you want to do this operation for particular document only
        unwind("DATA_LIST"),
        match(Criteria.where("DATA_LIST.status").is("DELIVRD"))
        project("DATA_LIST._id")
    );

AggregationResults<String> groupResults
        = mongoTemplate.aggregate(aggregation, "GroupIdDeliveryStatusReport", String.class);
    int count = groupResults.getMappedResults().size();