MongoDB聚合中映射结果与原始结果不同 [英] Mapped result is different from raw result in mongoDB aggregation

查看:44
本文介绍了MongoDB聚合中映射结果与原始结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正确的mongo查询的外观-

db.getCollection('product').aggregate([
{ "$match": { "itemSku": "TOS-16000-00623-00013" } },

{ "$unwind": "$priceInfoList" },

{ "$match": { "priceInfoList.communityId": 3  }},

 { "$group": {
        "_id": "$_id",
        "priceInfoList": { "$push": "$priceInfoList" }
    }}

])

这是实际产出和预期产出-

/* 1 */
{
    "_id" : ObjectId("61d815d0538792764c2df576"),
    "priceInfoList" : [ 
        {
            "listPrice" : 0.0,
            "offerPrice" : 0.0,
            "zoneId" : NumberLong(738),
            "communityId" : NumberLong(3),
            "storeType" : "Retailer",
            "principalGroup" : "DRP",
            "isActive" : true
        }, 
        {
            "listPrice" : 0.0,
            "offerPrice" : 0.0,
            "zoneId" : NumberLong(606),
            "communityId" : NumberLong(3),
            "storeType" : "Wholeseller",
            "principalGroup" : "DRP",
            "isActive" : true
        }, 
        {
            "listPrice" : 0.0,
            "offerPrice" : 0.0,
            "zoneId" : NumberLong(606),
            "communityId" : NumberLong(3),
            "storeType" : "Retailer",
            "principalGroup" : "NonDRP",
            "isActive" : true
        }
    ]
}

现在我正尝试使用Mongo模板在Spring Boot中实现同样的事情,但我从映射结果中得到的输出与原始结果不同。 请查看截图。

您可以看到,在映射结果中,我只得到一个priceInfo,而且也有空值,但我的原始结果是正确的。 我知道映射结果返回的是PriceInfo而不是List。以下是我在春季使用mongo模板编写的代码

public List<PriceInfo> findByCommunityOrPrincipalGroupOrStoreOrZone(String SKU, Long communityId, String principalGroup, String storeType, Long zoneId) {
    Criteria criteria = generateCriteriaToFilterSegments(communityId,principalGroup,storeType,zoneId);
    MatchOperation matchOperation = Aggregation.match(criteria);

    MatchOperation matchOperationSKU = Aggregation.match(Criteria.where("itemSku").is(SKU));

    UnwindOperation unwind = Aggregation.unwind("priceInfoList");

    GroupOperation groupOperation = Aggregation.group().push("priceInfoList").as("priceInfoList");

    Aggregation aggregation = Aggregation.newAggregation(matchOperationSKU,unwind,matchOperation,groupOperation);

    AggregationResults<PriceInfo> priceInfosAggregationResults
            = mongoTemplate.aggregate(aggregation, "product", PriceInfo.class);
    return priceInfosAggregationResults.getMappedResults();
  }

  private Criteria generateCriteriaToFilterSegments(Long communityId, String principalGroup, String storeType, Long zoneId) {
    Criteria criteria = new Criteria();
    if(communityId != null) {
      criteria.and("priceInfoList.communityId").is(communityId);
    }
    if(principalGroup != null) {
      criteria.and("principalGroup").is(principalGroup);
    }
    if(storeType != null) {
      criteria.and("storeType").is(storeType);
    }
    if(zoneId != null) {
      criteria.and("zoneId").is(zoneId);
    }
    return criteria;
  }

我如何更正它?

编辑-1

推荐答案

不是真正的答案,而是

db.getCollection('product').aggregate([
   { "$match": { "itemSku": "TOS-16000-00623-00013" } },
   { "$unwind": "$priceInfoList" },
   { "$match": { "priceInfoList.communityId": 3 } },
   {
      "$group": {
         "_id": "$_id",
         "priceInfoList": { "$push": "$priceInfoList" }
      }
   }
])

我会试试这个:

db.getCollection('product').aggregate([
   { $match: { itemSku: "TOS-16000-00623-00013" } },
   {
      $project: {
         priceInfoList: {
            $filter: {
               input: "$priceInfoList",
               cond: { $eq: ["$$this.communityId", 3] }
            }
         }
      }
   }
])

结果应该是相同的,并且可能更容易将其映射到Spring。我对春季数据不是不熟悉。

这篇关于MongoDB聚合中映射结果与原始结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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