【问题标题】:MongoDb aggregate query three collectionMongoDb聚合查询三合集
【发布时间】:2021-04-19 05:12:33
【问题描述】:

我对 MongoDB 查询和练习 mongoDb 查询真的很陌生。我正在使用这个官方mongoDb-package。我正在关注这个doc。我有三个数据集合。一个是包包,一个是卖家,最后一个是布料。我们的数据架构是当卖家发送带有他/她信息的布袋时。我们创建了这样的卖家集合:

{   "sellerId": 1234,
    "firstName": "John",
    "lastName": "doe",
    "fullName": "John Doe",
    "email": "john@yahoo.com",
    "bagId": 2224
}

这是包集合

 { 
   "sellerId": 1234
    "bagId": 2224,
    "source" : "fedex"
}

从袋子里挑选出打算出售的布料后,我们创建布料系列。

[
{
    "bagId": 2224,
    "brandName": "Denim",
    "size": "32",
    "clothId": 1244,
    "color": "green",
    "price": 20
},
{
    "bagId": 2224,
    "brandName": "Zara",
    "size": "31",
    "clothId": 1243,
    "color": "red",
    "price": 90
}
]

当布料从 Shopify 售出时。我们得到 SKU-ID 数组,这是我们的布料集合clothId。

我的目标是当布料售出时,我们将clothId(SKU-ID FROM SHOPIFY) 匹配到find bagId,从那个bagId我们会得到卖家信息。

我的预期结果是

{

    "firstName": "John",
    "lastName": "doe",
    "fullName": "John Doe",
    "email": "john@yahoo.com",
    "bagId": 2224,
    "clothId": 1244 // sold cloth id which I got from Shopify
    "sellerId": 1234
}

我成功匹配了售出的布料 id 和 shopify (SKU-ID) 并获取了包包信息,但我无法弄清楚如何从 bagId 获取卖家信息

这是我获取已售布料信息和包包详细信息的代码,但它没有给我卖家信息,只是得到了空数组

const sllerInfo = await client
    .db()
    .collection('clothes')
    .aggregate(
      [
        { $match: { clothId: { '$in': convertInto } } }, // convertInto is arrays of sold clothId which I got from Shopify
        {
          $lookup:
          {
            from: "bags",
            localField: "bagId",
            foreignField: "bagId",
            as: "bags"
          }
        },
        {
          $lookup:
          {
            from: "sellers",
            localField: "sellerId",
            foreignField: "sellerId",
            as: "sellers"
          },
        },
        {
          "$project": {
            "bagId": 1.0,
            "bags.source": 1.0,
            "sellers.firstName": 1.0, // dont get anything
            "sellers.lastName": 1.0,  // dont get anything
            "brand": 1.0
          }
        },
      ]
    ).toArray()

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    演示 - https://mongoplayground.net/p/MjGG_8HLT1T

    你必须使用$unwind,你需要从bags.sellerId获取卖家id

    db.clothes.aggregate([
      {
        $match: {
          clothId: {
            "$in": [
              1244,
              1243
            ]
          }
        }
      },
      {
        $lookup: {
          from: "bags",
          localField: "bagId",
          foreignField: "bagId",
          as: "bags"
        }
      },
      {
        $unwind: "$bags" // break into individual documents
      },
      {
        $lookup: {
          from: "sellers",
          localField: "bags.sellerId", // you need to get seller id from bags
          foreignField: "sellerId",
          as: "sellers"
        },
        
      },
      {
        $unwind: "$sellers"  // break into individual documents
      },
      {
        "$project": {
          _id: 0,
          "clothId": 1,
          "sellerId": "$sellers.sellerId",
          "bagId": 1,
          "bags.source": 1,
          "sellers.firstName": 1,
          "sellers.lastName": 1,
          "brand": 1
        }
      }
    ])
    

    如果您知道查找中只有唯一匹配的元素,您可以跳过展开。

    演示 - https://mongoplayground.net/p/IP0epMpQ6mE

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 2023-03-28
      • 2019-06-18
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多