而且Criteria中的Operator对于聚合Spring Data Mongo中的嵌套文档无法按预期工作

时间:2019-01-27 10:42:56

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

我正在尝试获取全部答复,其中答复的读取值为true。但是我通过Spring Data Mongo中可用的Aggregation函数将计数值设为3,但预期值为2(因为只有两个读取值为true)。下面是我编写的代码:

Aggregation sumOfRepliesAgg = newAggregation(match(new Criteria().andOperator(Criteria.where("replies.repliedUserId").is(userProfileId),Criteria.where("replies.read").is(true))),
        unwind("replies"), group("replies").count().as("repliesCount"),project("repliesCount"));
AggregationResults<Comments> totalRepliesCount = mongoOps.aggregate(sumOfRepliesAgg, "COMMENTS",Comments.class);
    return totalRepliesCount.getMappedResults().size();

在“条件查询”中使用AND运算符,并通过了两个条件,但未按预期运行。下面是示例数据集:

{
"_id" : ObjectId("5c4ca7c94807e220ac5f7ec2"),
"_class" : "com.forum.api.domain.Comments",
"comment_data" : "logged by karthe99",
"totalReplies" : 2,
"replies" : [ 
    {
        "_id" : "b33a429f-b201-449b-962b-d589b7979cf0",
        "content" : "dasdsa",
        "createdDate" : ISODate("2019-01-26T18:33:10.674Z"),
        "repliedToUser" : "@karthe99",
        "repliedUserId" : "5bbc305950a1051dac1b1c96",
        "read" : false
    }, 
    {
        "_id" : "b886f8da-2643-4eca-9d8a-53f90777f492",
        "content" : "dasda",
        "createdDate" : ISODate("2019-01-26T18:33:15.461Z"),
        "repliedToUser" : "@karthe50",
        "repliedUserId" : "5c4bd8914807e208b8a4212b",
        "read" : true
    }, 
    {
        "_id" : "b56hy4rt-2343-8tgr-988a-c4f90598h492",
        "content" : "dasda",
        "createdDate" : ISODate("2019-01-26T18:33:15.461Z"),
        "repliedToUser" : "@karthe50",
        "repliedUserId" : "5c4bd8914807e208b8a4212b",
        "read" : true
    }
],
"last_modified_by" : "karthe99",
"last_modified_date" : ISODate("2019-01-26T18:32:41.394Z")
}

我写的查询中有什么错误?

0 个答案:

没有答案