基于带查找的嵌套属性起草聚合以分组

问题描述 投票:0回答:1

有下面这两个合集

orders: [
  {
    "_id": "64355c928dcce8cdf4b9c7d2",
    "destinations": [
      {
        "ship_to_id": "64355c92af10d37993473e12", // mongoose id from locations collection
        "sold_to_id": "64355c92a57d8b29412a1cc2" // mongoose id from locations collection
      },
      {
        "ship_to_id": "64355c92a57d8b29412a1cc2",
        "sold_to_id": "64355c92ed8af3f7cd7199b2"
      },
      {
        "ship_to_id": "64355c92256aa652e6c3fdc5",
        "sold_to_id": "64355c924f9a2fcafa90daed"
      }
    ],
    "contact_details": [
      {
        "ship_to_ref": "aaa",
        "sold_to_ref": "bbb",
        "contact_email": "[email protected]",
      },
      {
        "ship_to_ref": "bbb",
        "sold_to_ref": "ccc",
        "contact_email": "[email protected]"
      },
      {
        "ship_to_ref": "ddd",
        "sold_to_ref": "eee",
        "contact_email": "[email protected]",
      }
    ]
  },
  {
    "_id": "64355c92bf25e54cf901be39",
    "destinations": [
      {
        "ship_to_id": "64355c92af10d37993473e12",
        "sold_to_id": "64355c92a57d8b29412a1cc2"
      },
      {
        "ship_to_id": "64355c92a57d8b29412a1cc2",
        "sold_to_id": "64355c92ed8af3f7cd7199b2"
      },
      {
        "ship_to_id": "64355c92af10d37993473e12",
        "sold_to_id": "64355c92256aa652e6c3fdc5"
      }
    ],
    "contact_details": [
      {
        "ship_to_ref": "aaa",
        "sold_to_ref": "bbb",
        "contact_email": "[email protected]",
      },
      {
        "ship_to_ref": "bbb",
        "sold_to_ref": "ccc",
        "contact_email": "[email protected]",
      },
      {
        "ship_to_ref": "aaa",
        "sold_to_ref": "ddd",
        "contact_email": "[email protected]",
      }
    ]
  },
  {
    "_id": "64355c921445785f4b50040e",
    "destinations": [
      {
        "ship_to_id": "64355c92af10d37993473e12",
        "sold_to_id": "64355c92a57d8b29412a1cc2"
      },
      {
        "ship_to_id": "64355c92af10d37993473e12",
        "sold_to_id": "64355c92ed8af3f7cd7199b2"
      }
    ],
    "contact_details": [
      {
        "ship_to_ref": "aaa",
        "sold_to_ref": "bbb",
        "contact_email": "[email protected]",
      },
      {
        "ship_to_ref": "aaa",
        "sold_to_ref": "ccc",
        "contact_email": "[email protected]",
      },
    ]
  }
]

locations: [
  {
    "_id": "64355c92af10d37993473e12",
    "reference_id": "aaa"
  },
  {
    "_id": "64355c92a57d8b29412a1cc2",
    "reference_id": "bbb"
  },
  {
    "_id": "64355c92ed8af3f7cd7199b2",
    "reference_id": "ccc"
  },
  {
    "_id": "64355c92256aa652e6c3fdc5",
    "reference_id": "ddd"
  },
  {
    "_id": "64355c924f9a2fcafa90daed",
    "reference_id": "eee"
  },
]

现在我想要实现的是编写一个聚合查询,根据相同的 ship_to、sold_to 和 contact_email 对我的所有缩进进行分组(Note:要验证这是否是有效的 ship_to 和 sold_to,我们需要从

locations
集合中查找使用
ship_to_id
sold_to_id
,检索各自的
reference_id 's
,然后将其与联系人详细信息中可用的
ship_to_ref
sold_to_ref
进行比较)这样查询结果就像

[{
  _id: {
    ship_to_ref: "aaa",
    sold_to_ref: "bbb",
    ship_to_id:"64355c92af10d37993473e12"
    sold_to_id:"64355c92a57d8b29412a1cc2"
    contact_email: "[email protected]"
   },
   orders: [
     { "_id": "64355c928dcce8cdf4b9c7d2", ... },
     { "_id": "64355c92bf25e54cf901be39", ... },
     { "_id" : "64355c921445785f4b50040e", ...}
   ],
 },
 {
  _id: {
    ship_to_ref: "bbb",
    sold_to_ref: "ccc",
    ship_to_id:"64355c92a57d8b29412a1cc2",
    sold_to_id:"64355c92ed8af3f7cd7199b2",
    contact_email: "[email protected]"
   },
   orders: [
     { "_id": "64355c928dcce8cdf4b9c7d2", ... },
     { "_id": "64355c92bf25e54cf901be39", ... },
   ],
 },
 {
  _id: {
    ship_to_ref: "ddd",
    sold_to_ref: "eee",
    ship_to_id: "64355c92256aa652e6c3fdc5",
    sold_to_id: "64355c924f9a2fcafa90daed",
    "contact_email": "[email protected]",
   },
   orders: [
     { "_id": "64355c928dcce8cdf4b9c7d2", ... }
   ],
 },
{
  _id: {
    ship_to_ref: "aaa",
    sold_to_ref: "ddd",
    ship_to_id: "64355c92af10d37993473e12",
    sold_to_id: "64355c92256aa652e6c3fdc5",
    contact_email: "[email protected]",
   },
   orders: [
     { "_id": "64355c92bf25e54cf901be39", ... }
   ],
 },
{
  _id: {
    ship_to_ref: "aaa",
    sold_to_ref: "ccc",
    ship_to_id: "64355c92af10d37993473e12",
    sold_to_id: "64355c92ed8af3f7cd7199b2",
    contact_email: "[email protected]",
   },
   orders: [
     { "_id": "64355c921445785f4b50040e", ... }
   ],
 },
]

下面是我试过的聚合查询

[{
  $match: {
    contact_details: { $exists: true },
  },
},
{ $unwind: '$destinations' },
{ $unwind: '$contact_details'},
{
  $group: {
    _id: {
      sold_to_id: '$destinations.sold_to_id',
      ship_to_id: '$destinations.ship_to_id',
    }
    orders: {
      $push: {
        id: '$order_no', // order_no is an attribute from an order object
        contact_details: '$contact_details',
      },
    },
    type: { $first: { type: '$destinations.type' } },
  },
}]

但不确定如何合并查找并进行参考比较和电子邮件检查。 需要帮助。

提前致谢

javascript mongodb mongodb-query aggregation-framework lookup
1个回答
0
投票

一个选项是

$map
在你
unwind
之前,所以你只能
unwind
一次并使用
$indexOfArray
以匹配
$lookup
之后的引用:

db.orders.aggregate([
  {$match: {contact_details: {$exists: true}}},
  {$project: {
      del: {$map: {
          input: {$range: [0, {$size: "$contact_details"}]},
          in: {
            destination: {$arrayElemAt: ["$destinations", "$$this"]},
            contact: {$arrayElemAt: ["$contact_details", "$$this"]}
          }
      }}
  }},
  {$unwind: "$del"},
  {$group: {
      _id: {
        sold_to_id: "$del.destination.sold_to_id",
        ship_to_id: "$del.destination.ship_to_id"
      },
      contact_email: {$first: "$del.contact.contact_email"},
      orders: {$push: {contact_details: "$del.contact"}}
  }},
  {$set: {locations: ["$_id.sold_to_id", "$_id.ship_to_id"]}},
  {$lookup: {
      from: "locations",
      localField: "locations",
      foreignField: "_id",
      as: "locations"
  }},
  {$project: {
      "_id.ship_to_id": "$_id.ship_to_id",
      "_id.sold_to_id": "$_id.sold_to_id",
      "_id.contact_email": "$contact_email",
      "_id.ship_to_ref": {$getField: {
          input: {$arrayElemAt: [
              "$locations",
              {$indexOfArray: ["$locations._id", "$_id.ship_to_id"]}
          ]},
          field: "reference_id"
      }},
      "_id.sold_to_ref": {$getField: {
          input: {$arrayElemAt: [
              "$locations",
              {$indexOfArray: ["$locations._id", "$_id.sold_to_id"]}
          ]},
          field: "reference_id"
      }},
      orders: 1
  }}
])

游乐场示例中查看它是如何工作的

© www.soinside.com 2019 - 2024. All rights reserved.