拥有“Orders”集合和“Order_Items”是“Orders”集合中的数组。
“Order_Item”(数组)具有以下元素:
{
"line_number": "611194",
"split_number": "1",
},
{
"line_number": "611194",
"split_number": "1",
},
{
"line_number": "611194",
"split_number": "2",
},
{
"line_number": "611194",
"split_number": "3",
},
{
"line_number": "611194",
"split_number": "3",
},
所以,我必须为常见的Split_Number创建一个单独的“Orders”,如:
订单ID:1
{
"line_number": "611194",
"split_number": "1",
},
{
"line_number": "611194",
"split_number": "1",
},
订单ID:2
{
"line_number": "611194",
"split_number": "2",
},
订单ID:3
{
"line_number": "611194",
"split_number": "3",
},
{
"line_number": "611194",
"split_number": "3",
},
以下代码使用:
var result = lines.reduce((p, c) => {
const key = `Order_Items_${c}`;
p[key] = p[key] || [];
p[key].push(c)
return p;
}, {});
答案 0 :(得分:1)
免责声明:很难准确理解你所提出的问题,但我认为这个答案提供了你想要完成的事情的本质。
您可以使用mongo Aggregation框架实现此目的:
db.getCollection('OrderItems').aggregate([{ $group:
{
_id: "$split_number",
line_numbers: { $push: "$line_number" }
}
}])
这将通过split_number返回所有订单和组。然后为您提供链接到该split_number的行号集合。