MongoDB的聚合和比较

时间:2017-11-16 18:56:19

标签: mongodb nosql mongodb-query aggregation-framework nosql-aggregation

我需要编写mongodb查询,通过quantinty(orders collestion)来聚合项目。在聚合或使用聚合之后,它应该识别在库存收集中不能提供的物品,并返回下订单的人的姓名

订单代码

db.orders.insertMany([
{client:  {firstName: "John",lastName: "Smith" },orderedItems: [{ name: "itemA", qty: 5},{ name: "itemS", qty: 3}]},
{client:  {firstName: "Jan",lastName: "Nowak" },orderedItems: [{ name: "itemA", qty: 56},{ name: "itemS", qty: 53}]},
{client:  {firstName: "Klara",lastName: "Bolączka" },orderedItems: [{ name: "itemA", qty: 35},{ name: "itemS", qty: 23}]},
{client:  {firstName: "Brajan",lastName: "Kowalski" },orderedItems: [{ name: "itemA", qty: 95},{ name: "itemS", qty: 13}]}
]);

股票代码

db.stock.insertMany([
{ item: "itemA", qty: 40},
{ item: "itemS", qty: 113}  
]); 

现在我得到了一些mongoDB代码,只是简单地命令

db.orders.aggregate([
{$unwind: "$orderedItems"},
{$group: { _id: "$orderedItems.name", total:{$sum: "$orderedItems.qty"}}}
]);

我尝试使用查找,但可能我做错了。

我可以得到一些消化吗?

1 个答案:

答案 0 :(得分:0)

你想要这样的东西:

afterEvaluate