聚合mongodb在前端显示数据

时间:2016-06-03 20:41:19

标签: mongodb aggregation-framework

我是一名全新的学生,可以进行全面的JavaScript开发。我目前正在开发一个项目,通过食品订购界面生成库存管理信息。当前流程是订购的每个项目都是一个被推入数组的对象,该数组代表完整的订单。

例如:它进入:

    {
    "_id" : ObjectId("5751e8c495251b62210242c2"),
    "newMenu" : [
            {
                    "checked" : true,
                    "amount" : 1,
                    "stockNum" : 105,
                    "price" : 11.25,
                    "desc" : "Slow cooked ribs that slide right off the bone. Covered in Jim's special BBQ sauce.",
                    "item" : "Baby Back Ribs"
            },
            {
                    "checked" : true,
                    "amount" : 1,
                    "stockNum" : 107,
                    "price" : 7.85,
                    "desc" : "Delicious pasta stuffed with spinach and ricotta cheese.",
                    "item" : "Raph's Ravioli"
            },
            {
                    "checked" : true,
                    "amount" : 1,
                    "stockNum" : 200,
                    "price" : 1.5,
                    "desc" : "Perpsi, Croke, Mountain Mist, Nurse Pepper, or Spreete.",
                    "item" : "Fountain Drink"
            },
            {
                    "checked" : true,
                    "amount" : 1,
                    "stockNum" : 204,
                    "price" : 5.25,
                    "desc" : "A delicious brown ale from the UK.",
                    "item" : "Oldcastle"
            },
            {
                    "checked" : true,
                    "amount" : 1,
                    "stockNum" : 206,
                    "price" : 4.5,
                    "desc" : "American pale lager.",
                    "item" : "Dudweiser"
            },
            {
                    "checked" : true,
                    "amount" : 1,
                    "stockNum" : 207,
                    "price" : 4.5,
                    "desc" : "Light American pale lager.",
                    "item" : "Dud Light"
            },
            {
                    "checked" : true,
                    "amount" : 1,
                    "stockNum" : 101,
                    "price" : 12.8,
                    "desc" : "Thin crusted pizza with your choice of toppings.",
                    "item" : "New York Pizza"
            }
    ],
    "__v" : 0
 }

我需要根据库存号或stockNum获取订购的每件商品的金额并返回该值。

到目前为止,我已经尝试过:

db.menus.aggregate([ { $match: { stockNum: "208" } }, {$group: {_id: "$cust_id", total: {$sum: "$amount"}}}, {$sort: {total: -1}}])

虽然我没有错误,但我也没有输出。

谢谢!

0 个答案:

没有答案