Spring Data MongoDb是否支持$ filter数组聚合运算符? [英] Does Spring Data MongoDb support $filter array aggregations operator?

查看:570
本文介绍了Spring Data MongoDb是否支持$ filter数组聚合运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MongoTemplate在Spring Data中实现以下工作的mongoDb查询:

I'm trying to implement in Spring Data using MongoTemplate the following working mongoDb query:

db.answers.aggregate([
        { "$match" : { "object_id" : "1" } },
        { "$project": { 'answer_list': 1, 'profile': { $filter : { input: '$answer_list', as: 'answer', cond: { $eq: [ '$$answer.question', 2 ] } } } } },
        { "$unwind" : "$profile"},
        { "$unwind" : "$answer_list"},
        { "$group" : { "_id" : { "question" : "$answer_list.question", "answer" : "$answer_list.answer", "criteria" : "$profile.answer"}, "count" : { "$sum" : 1 } } },
        { "$sort" : { "_id.question" : 1, "_id.answer" : 1 } }
]);

该集合具有以下结构:

{
"_id" : ObjectId("..."),
"object_id" : ObjectId("..."),
"answer_list" : [ 
    {
        "question" : NumberLong(0),
        "answer" : NumberLong(0)
    }, 
    {
        "question" : NumberLong(1),
        "answer" : NumberLong(2)
    }, 
    {
        "question" : NumberLong(2),
        "answer" : NumberLong(2)
    }
]}

我想在这里做的是一份关于简单调查提交数据的报告.问题是第一个问题的回答为0的用户如何回答第二个问题?" 我花了一整天的时间搜索SpringData Mongo Db文档,但没有发现任何东西. 有人可以帮忙吗?

What I'm trying to do here is a report on a simple survey submission data. The question is "How did the users that answered 0 to the first question answer to the second question?" I spent all day searching the SpringData Mongo Db docs but I found nothing. Can anyone help?

TIA

推荐答案

您可以通过提供自己的AggregationExpression来解决此问题.

You can workaround this issue by providing your own AggregationExpression.

ProjectionOperation agg = Aggregation.project() //

      .and(new AggregationExpression() {

        @Override
        public DBObject toDbObject(AggregationOperationContext context) {

          DBObject filterExpression = new BasicDBObject();
          filterExpression.put("input", "$answer_list");
          filterExpression.put("as", "answer");
          filterExpression.put("cond", new BasicDBObject("$eq2", Arrays.<Object> asList("$$answer.question", 2)));

          return new BasicDBObject("$filter", filterExpression);
        }
      }).as("profile");

这篇关于Spring Data MongoDb是否支持$ filter数组聚合运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆