如何根据第一次收集的结果从第二个集合中获取数据 - mongodb

时间:2018-02-13 07:57:51

标签: mongodb aggregation-framework

我有两个名为brandsoffers的集合。我需要编写一个查询来显示与分支和查询输出相关的总报价,我还需要在BrandModel中显示基于uuid的brandName,两者都在一个查询中。它可以在一个查询中完成吗?如果是,那么帮助将不胜感激。收集的模型和预期结果的快照如下:

BrandModel
{
    "uuid" : "1",
    "brandName" : "Siyarams",
    "addedOn" : 1511336968608,
    "contactPerson" : "Kapoor",
    "primaryContactNo" : "9999999999",
    "alternateContactNo" : "9999999999",
    "address" : "57th main",
    "landmark" : "Near KFC",
    "city" : "Mumbai",
    "brandStatus" : 3,
    "lastEdited" : 1511347444340
}

OfferModel
{
    "offerId" : "2",
    "brandUuid" : ["1"],
    "storeUuid" : ["1", "2", "3", "4"],
    "startTime" : 1427985798581,
    "endTime" : 1489846620149,
    "offerDesc" : "Nunc nisl.",
    "criteria" : "Nulla ac enim.",
    "minShoppingAmount" : 72,
    "maxShoppingAmount" : 13,
    "minCashbackAmount" : 166,
    "maxCashbackAmount" : 33,
    "offerStatus" : 2,
    "addedOn" : 1464960534465
}

预期结果

{
  "totalCount": 6,
  "offerList": [
    {
      "offerId": "2",
      "startTime": 1458414172009,
      "endTime": 1432239091529,
      "offerStatus": 2,
      "brandUuid": "1",
      "brandName": "Reebok",
      "storeCount": 4
    },
    {
      "offerId": "3",
      "startTime": 1502408506014,
      "endTime": 1418943623940,
      "offerStatus": 2,
      "brandUuid": "44",
      "brandName": "Adidas",
      "storeCount": 3
    },
    {
      "offerId": "4",
      "startTime": 1460451658862,
      "endTime": 1431555556723,
      "offerStatus": 1,
      "brandUuid": "30",
      "brandName": "Levis",
      "storeCount": 3
    },
    {
      "offerId": "5",
      "startTime": 1477083967093,
      "endTime": 1433758573895,
      "offerStatus": 1,
      "brandUuid": "32",
      "brandName": "Caterpilar",
      "storeCount": 2
    }
  ]
}

已编辑:我根据@ Raul的回答得到了结果,现在我的查询如下:

db.coll.aggregate([
  {
    $facet: {
      totalCount: [
        {
          $count: "count"
        }
      ],
      offerList: [
        {
          $match: {
            offerStatus: 2
          }
        },
        {
          $unwind: "$brandUuid"
        },
        {
          $project: {
            _id: 0,
            offerId: 1,
            brandUuid: 1,
            storeCount: {
              $size: "$storeUuid"
            },
            startTime: 1,
            endTime: 1,
            offerStatus: 1
          }
        },
        {
          $lookup: {
            from: "brands",
            localField: "brandUuid",
            foreignField: "brandUuid",
            as: "brand"
          }
        },
        {
          $addFields: {
            brand: {
              $arrayElemAt: [
                "$brand.brandName",
                0
              ]
            }
          }
        }
      ]
    }
  },
  {
    $addFields: {
      totalCount: {
        $arrayElemAt: [
          "$totalCount.count",
          0
        ]
      }
    }
  }
])

1 个答案:

答案 0 :(得分:1)

这可以帮助您,在一个查询中使用聚合。只需检查您是否输入正确或为什么将brandUuid作为数组?这意味着您的优惠可能有多个品牌,在这种情况下请咨询$ unwind阶段。

db.BrandModel.aggregate([
   {
        $lookup: {
            from: "OfferModel",
            localField: "uuid",
            foreignField: "brandUuid",
            as: "offerList"
        }
    } 
]);

更新(从包含数组的集合中展开):

 db.OfferModel.aggregate([
   {
        $unwind: "$brandUuid"
   },
   {
        $lookup: {
            from: "BrandModel",
            localField: "brandUuid",
            foreignField: "uuid",
            as: "brand"
        }
   },
   {
        $group : { _id : "$brand" }
   }
]);