【问题标题】:Need to get all fields along with Aggregate field without using Include Fields in Query需要在不使用查询中包含字段的情况下获取所有字段以及聚合字段
【发布时间】:2017-10-09 05:37:09
【问题描述】:

我们尝试了 $project (Aggregation) 和 Include Fields(Query),但它只包括 Id 和 Filtered 字段。我们预期的结果是文档中的所有字段以及嵌入文档都按区域设置(动态键)过滤。

例子:

categories:[{
_id: 1,
categoryNumber: "12345",
locale: {
                en_US: {
                                name: "Category Name in US"
                },
                fr_FR: {
                                name: "Category Name in French"
                }
}
},
{
_id: 2,
categoryNumber: "6789",
locale: {
                en_GB: {
                                name: "Category Name in UK"
                }
}
}]

Expected Result:

Filter records by locale: en_US(Embedded document key)

[{
_id: 1,
categoryNumber: "12345",
locale: {
                en_US: {
                                name: "Category Name in US"
                }
}
}]

查询:

AggregationOperation matchOperaion = match(where(Constants.ID).is(clientProdTypeId));

                              AggregationExpression aggregationExpression = (AggregationOperationContext) -> {
                                             Map<String, Object> conditionMap = new HashMap<>();
                                             conditionMap.put("input", "$locale.en_US.name"); 
                                             conditionMap.put("as", Constants.NAME);
                                             conditionMap.put("cond", new BasicDBObject());
                                             return new BasicDBObject("$filter", conditionMap);
                              };

                              AggregationOperation projectionOperation;


                                             projectionOperation = project().and(aggregationExpression).as(Constants.METADATA)
                                                                           .andInclude(Constants.CLIENT_ID, Constants.PRODUCT_TYPE_ID,Constants.STATUS);


                              AggregationResults<CategoryDTO> result;

                              try {
                                             result = mongoOperations.aggregate(newAggregation(matchOperaion, projectionOperation),
                                                                           Category.class, CategoryDTO.class);
                              } catch (IllegalArgumentException | DataAccessException e) {
                                             LOGGER.error("Error while fetching client product type", e);

                              }
                              return result.getUniqueMappedResult();

【问题讨论】:

  • 您的聚合查询在哪里?
  • 我已经更新了问题

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


【解决方案1】:

使用$addFields 代替$project$project 仅传递指定的字段,而 $addFields 传递所有字段以及新的/计算的/字段。

【讨论】:

    【解决方案2】:

    您可以在3.4 版本中使用以下聚合表达式。

    $objectToArraylocale 转换为键值对,$filter 用于语言环境输入,$arrayToObject 转换回动态键。

      AggregationExpression aggregationExpression = (AggregationOperationContext) -> {
         Map<String, Object> conditionMap = new HashMap<>();
         conditionMap.put("input", new BasicDBObject("$objectToArray", "$locale"));
         conditionMap.put("as", "result");
         conditionMap.put("cond", new BasicDBObject("$$result.k", "en_US"));
         return new BasicDBObject("$arrayToObject", new BasicDBObject("$filter", conditionMap));
       };
    

    【讨论】:

      【解决方案3】:
      db.collection.find({locale.en_US:{$exists : true}});
      

      【讨论】:

      • 它将按区域设置过滤记录:en_US(Embedded document key)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 2018-01-18
      • 2014-02-25
      • 2013-08-29
      相关资源
      最近更新 更多