【问题标题】:MongoDB agregation and comparingMongoDB聚合和比较
【发布时间】:2017-11-16 18:56:18
【问题描述】:

我需要编写 mongodb 查询,该查询将按数量(订单集合)聚合项目。汇总后或汇总后,应识别库存集合中无法供应的物品,并返回下订单人的姓名

订单代码插入

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"}}}
]);

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

请给我一些建议好吗?

【问题讨论】:

  • 我想说你做错的第一件事就是完全忽略了你所问问题的任何使用尝试。我们不在乎您是否错了,而只在乎您实际尝试过。在我看来,"orders" 集合中的"localField" 是"orderedItems.name"。从我的立场来看,如果你真的尝试了一些东西,那么你可以问一个具体的问题。但这不是您在这里问的问题,我所看到的只是“请为我做”。如果您发现它,很乐意帮助解释您不理解的部分。
  • 很抱歉耽误了您的时间...我试过了,并用 $lookup 写了一些声明,但编辑了它,因为认为它很愚蠢...我身体不舒服,得到几乎一直头痛。我真的很努力,但你知道... db.orders.aggregate([ {$project : { _id: 0, client : 1 , orderedItems : 1 }}, {$unwind: "$orderedItems"}, {$group : { _id: "$orderedItems.name", total:{$sum: "$orderedItems.qty"}}}, {$lookup:{from: "stock", localField: 'orderedItems.name', foreignField: "item ", as: "a"}}, {$match: { "a": { $ne: [] } }} ]);
  • 没有“尝试”in your question。您需要向我们展示您的尝试。您还需要注意评论中非常清晰的说明,该说明基本上向您展示了第一步是什么。询问“Gimmie the code”的问题是此处关闭的原因。在实际解决它时付出一些努力,你会有所收获。
  • 看看我的帖子的早期版本

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


【解决方案1】:

你喜欢这样的东西吗:

db.orders.aggregate([
{
    $unwind: "$orderedItems" // flatten the "orderedItems" array
},
{
    $group: {
        "_id": "$orderedItems.name", // group by orderItems.name
        "total": {
            $sum: "$orderedItems.qty" // calculate total quantity
        },
        "users": {
            $addToSet: "$client" // create an array of clients who have ordered each item
        }
    }
}, {
    $lookup: { // retrieve the stock information
       from: "stock",
       localField: "_id",
       foreignField: "item",
       as: "stock"
    }
}, {
    $unwind: "$stock" // flatten the result array - should always return only one document, given your data model
}, {
    $addFields: { // add a new field...
        "difference": { // ...called "difference"...
            $subtract: [ "$stock.qty", "$total" ] // ...in which we store the difference between the stock qty and the ordered qty
        }
    }
}, {
    $match: { // only return documents...
        "difference": { // ...where the value for "difference"...
            $lt: 0 // ...is less than zero
        }
    }
}])

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    相关资源
    最近更新 更多