【发布时间】:2020-08-25 23:24:53
【问题描述】:
我正在尝试创建一个简单的客户/产品/订单数据库。 This is the customer collection
This is the product collection
我更喜欢使用 ref 键而不是嵌入文档只是为了锻炼。 我正在尝试按客户名称进行分组,然后为每个客户求和他的总订单数 = Sum (Product_price x Quantity)。
这是我尝试过的查询,但它不起作用:/
db.customers.aggregate([
{$lookup:{from:"orders", localField:"ordered",foreignField:"_id", as:"Bought"}},
{$lookup:{from:"products", localField:"Bought.Includes.product",foreignField:"_id", as:"User_products"}},
{$group:{_id:{name:"$name"},total: { $multiply: [ "$price", "$quantity" ] }}},
{$project: {name:1, total:1} }
]).pretty()
【问题讨论】:
-
您为订单和产品发布了相同的图片
-
没问题,欢迎来到 Stack Overflow。另一个提示:请避免发布数据图像,而是将数据作为文本发布在问题内容中。您可以使用编辑器格式化功能使其更具可读性。请参阅此处的指南以了解如何询问stackoverflow.com/help/how-to-ask