Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据第一次采集的结果从第二次采集中获取数据-mongodb_Mongodb_Aggregation Framework - Fatal编程技术网

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

如何根据第一次采集的结果从第二次采集中获取数据-mongodb,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有两个系列,分别命名为品牌和优惠。我需要编写一个查询来显示与分支机构关联的总报价,在查询的输出中,我还需要在BrandModel中基于uuid显示品牌名称,这两个查询都在单个查询中。可以在单个查询中完成吗?如果是的话,我们将不胜感激。集合和预期结果快照的模型如下所示: BrandModel { "uuid" : "1", "brandName" : "Siyarams", "addedOn" : 1511336968608, "contactPerson" :

我有两个系列,分别命名为
品牌
优惠
。我需要编写一个查询来显示与分支机构关联的总报价,在查询的输出中,我还需要在BrandModel中基于
uuid
显示品牌名称,这两个查询都在单个查询中。可以在单个查询中完成吗?如果是的话,我们将不胜感激。集合和预期结果快照的模型如下所示:

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
        ]
      }
    }
  }
])

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

db.BrandModel.aggregate([
   {
        $lookup: {
            from: "OfferModel",
            localField: "uuid",
            foreignField: "brandUuid",
            as: "offerList"
        }
    } 
]);
更新(从包含阵列的集合中展开):


你在运行什么版本的mongo?到目前为止,您的查询是什么样子的……我认为Mongodb聚合是在v2.4中引入的。但这是v3.4+是的,我的报价模型可能包含多个品牌,因此它是ArrayTanks@Raul,现在我得到了结果。我想问,在我重新定义的查询中,我包括了
$unwind
阶段和
$match
阶段,那么,这两个阶段的顺序是否重要?是的,将每个阶段视为FOR循环,第一个阶段的输出是下一个阶段的输入。我需要知道你的$匹配的目的,以便我可以指导你按什么顺序排列。我已经添加了查询,现在你可以告诉我吗@RaulI建议首先考虑您的设计在特定的brandUuid阵列中是否正确。试着在你的报价模型上做聚合阶段,从另一边攻击,我将编辑我的答案。
 db.OfferModel.aggregate([
   {
        $unwind: "$brandUuid"
   },
   {
        $lookup: {
            from: "BrandModel",
            localField: "brandUuid",
            foreignField: "uuid",
            as: "brand"
        }
   },
   {
        $group : { _id : "$brand" }
   }
]);