投影具有不同关键节点子文档的多个文档

时间:2018-09-12 05:15:42

标签: mongodb aggregation-framework

我有两个文档,如图所示。它们的共同因素是子文档中的一个节点(类型,veg_type)。我还为每个文档添加了相同的公共节点(udf_type,udf_veg_type)。一个是旧数据(具有关键节点蔬菜),另一个是新数据(具有关键节点蔬菜)。 如何投影蔬菜和蔬菜的组合数据(在关键节点蔬菜中)而没有type和veg_type节点?我使用user_id进行匹配。

Intended Output

{
  "user_id": 31,
  "veggies": [
    {
      "udf_type": "green_vegetables",
      "tot": 28560,
      "itms": [
        {
          "num": 1,
          "itm_det": {
            "name": "spinach",
            "qty": 18
          }
        }
      ],
      "chksum": "d1583afab3a04f4b32589cfa64392765n78782ff60a0e0dc24b295868083"
    },
    {
      "udf_type": "vegetables",
      "tot": 2860,
      "itms": [
        {
          "num": 1,
          "itm_det": {
            "name": "onion",
            "qty": 1
          }
        }
      ],
      "chksum": "e497c7b288e50e3be4c6bc676e4c849e4n5645n64a2d77748e185d7a1bce8c"
    },
    {
      "udf_veg_type": "green_vegetables",
      "tot": 2352000,
      "itms": [
        {
          "num": 1,
          "itm_det": {
            "name": "kale",
            "qty": 18
          }
        }
      ],
      "chksum": "87b239cd9b39baa48b4564b5754009a131f542622ba018f37cd1fdb5"
    }
  ]
}

{
  "_id" : ObjectId("1"),
  "user_id": 31,
  "veggies": [
    {
      "type": "green_vegetables",
      "desc": [
        {
          "udf_type": "green_vegetables",
          "tot": 28560,
          "itms": [
            {
              "num": 1,
              "itm_det": {
                "name": "spinach",
                "qty": 18
              }
            }
          ],
          "chksum": "d1583afab3a04f4b32589cfa64392765n78782ff60a0e0dc24b295868083"
        }
      ]
    },
    {
      "type": "vegetables",
      "desc": [
        {
          "udf_type": "vegetables",
          "tot": 2860,
          "itms": [
            {
              "num": 1,
              "itm_det": {
                "name": "onion",
                "qty": 1
              }
            }
          ],
          "chksum": "e497c7b288e50e3be4c6bc676e4c849e4n5645n64a2d77748e185d7a1bce8c"
        }
      ]
    }
  ]
}

{
  "_id" : ObjectId("2"),
  "user_id": 31,
  "vegetables": [
    {
      "veg_type": "green_vegetables",
      "desc": [
        {
          "udf_veg_type": "green_vegetables",
          "tot": 2352000,
          "itms": [
            {
              "num": 1,
              "itm_det": {
                "name": "kale",
                "qty": 18
              }
            }
          ],
          "chksum": "87b239cd9b39baa48b4564b5754009a131f542622ba018f37cd1fdb5"
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

假设desc数组始终只有个项目,请尝试以下操作:

db.collection.aggregate([
  {
    $match: {
      "user_id": 31 // change this into the user_id variable
    }
  },
  {
    $group: {
      _id: "$user_id",
      veggies: {
        $max: "$veggies"
      },
      vegetables: {
        $max: "$vegetables"
      }
    }
  },
  {
    $project: {
      "user_id": "$_id",
      "veggies": {
        $concatArrays: [
          {
            $cond: [
              "$veggies",
              {
                $map: {
                  input: "$veggies",
                  in: {
                    $arrayElemAt: [
                      "$$this.desc",
                      0
                    ]
                  }
                }
              },
              []
            ]
          },
          {
            $cond: [
              "$vegetables",
              {
                $map: {
                  input: "$vegetables",
                  in: {
                    $arrayElemAt: [
                      "$$this.desc",
                      0
                    ]
                  }
                }
              },
              []
            ]
          }
        ]
      }
    }
  }
])