Mongodb 相当于 RIGHT JOIN - 数据可能在集合 A 或 B 中,我应该合并集合还是查找两次?

Mongodb equivalent of RIGHT JOIN - Data may be in collection A or B, should I combine collections or lookup twice?

我是 MongoDB 的新手,来自 SQL 背景

我有两个订单集合:

我想为客户汇总数据,我面临的问题是我不能简单地进行查找,因为有些客户只会使用 B,因此他们将被排除在外

最好的方法是什么?

我犹豫是否要在客户级别创建一个全新的集合,嵌入 A 和 B,因为这会重复数据,space 是一个重要的考虑因素


这是 A 和 B 的示例:

A(网站):

{
"_id": 1,
customer_id: 2,
order_id: 1188,
date: "2018/06/01",
item_id: "1",
item_name: "Item 1",
amount_paid: 30
}

B(应用程序):

{
"_id": 12,
customer_id: 2,
order_id: 1247,
date: "2018/04/01",
item_id: "2",
item_name: "Item 2",
amount_paid: 8
},
{
"_id": 13,
customer_id: 4,
order_id: 2532,
date: "2018/08/02",
item_id: "2",
item_name: "Item 2",
amount_paid: 8
}

重要提示:绝大多数订单是使用网站 (A) 制作的

这里的最佳做法是什么?


我要回答的基本问题:

"How many orders are there for customer x?" 其中 x 可以使用 (A 和 B) 或 (A 或 B)


预期输出:

{
"_id": 1,
customer_id: 2,
order_id: 1188,
date: "2018/06/01",
item_id: "1",
item_name: "Item 1",
amount_paid: 30
},
{
"_id": 12,
customer_id: 2,
order_id: 1247,
date: "2018/04/01",
item_id: "2",
item_name: "Item 2",
amount_paid: 8
},
{
"_id": 13,
customer_id: 4,
order_id: 2532,
date: "2018/08/02",
item_id: "2",
item_name: "Item 2",
amount_paid: 8
}

在预期的输出中,您可以看到所有 customer_id 2 都有来自 A 和 B 的订单,并且还包括 customer_id 4(未出现在 A 中)


MongoDB 版本 - 4.0.1


提前致谢

我发现我可以简单地做到这一点:

db.B.find().forEach (
    function(x) {
        db.A.insert(x)
        } )

而它的作用是 "for every document in B, insert as a new document into collection A"