MongoDB聚合问题

时间:2018-04-18 12:57:10

标签: database mongodb aggregate

我正在尝试使用聚合函数显示ID为'O1234'的订单中订购的所有产品。我已多次查看我的代码,并且一直收到错误消息,说我已经省略了'}'括号,但我似乎无法找到它。

以下是我为上述查询尝试过的代码:

db.order.aggregate([
    {$unwind: "$order"},
    {$unwind: "$order.product"}, 
    {$match: {"order._id": 'O1234'}}, 
    {
        $project: {
            _id:0, 
            prod_name: "$order.product.prod_name",
            prod_price: "$order.product.prod_price", 
            order_qty: "$order.product.order_qty", 
            order_total: $multiply:
            ["$order.product.prod_price", "$order.product.order_qty"]
        }
    }
]);

以下是我的订单集合代码,其中包含一系列产品:

db.order.insert(
    {
        _id: 'O1234',
        date: '2018-02-11',
        time: '09:03',
        cust_id: 'C2057',
        driver: 'Mike',
        products: [
            {   _id: 'P1234', 
                order_qty: 1,
                prod_name: 'Office Desk',
                prod_price: 50
            },
            {
                _id: 'P5678',
                order_qty: 1,
                prod_name: 'Bathroom Cabinet',
                prod_price: 30
            }
        ]
    }
);
db.order.insert(
    {
        _id: 'O5678',
        date: '2018-06-10',
        time: '07:30',
        cust_id: 'C2018',
        driver: 'Jack',
        products: [
            {
                _id: 'P1122', 
                order_qty: 5,
                prod_name: 'Outdoor Chair',
                prod_price: 65
            },
            {
                _id: 'P3344',
                order_qty: 3,
                prod_name: 'Dining Table',
                prod_price: 90
            },
            {
                _id: 'P5566',
                order_qty: 7,
                prod_name: 'Bathroom Cabinet',
                prod_price: 30
            },
        ]
    }
);
db.order.insert(
    {
        _id: 'O4321',
        date: '2018-11-07',
        time: '06:00',
        cust_id: 'C0987',
        driver: 'Jack',
        products: [
            {
                _id: 'P3344', 
                order_qty: 10,
                prod_name: 'Outdoor Chair',
                prod_price: 65
            }
        ]
    }
);
db.order.insert(
    {
        _id: 'O8765',
        date: '2017-08-19',
        time: '11:30',
        cust_id: 'C2018',
        driver: 'Mike',
        products: [
            {
                _id: 'P1234', 
                order_qty: 2,
                prod_name: 'Office Desk',
                prod_price: 50
            }
        ]
    }
);
db.order.insert(
    {
        _id: 'O2018',
        date: '2018-02-11',
        time: '20:43',
        cust_id: 'C1849',
        driver: 'Mike',
        products: [
            {
                _id: 'P1122', 
                order_qty: 10,
                prod_name: 'Outdoor Chair',
                prod_price: 65
            },
            {
                _id: 'P5678',
                order_qty: 2,
                prod_name: 'Bathroom Cabinet',
                prod_price: 30
            }
        ]
    }
);

提前致谢!

2 个答案:

答案 0 :(得分:0)

我认为多重语法将如下所述

total: { $multiply: [ "$price", "$quantity" ] } 

如需更多帮助,请参阅以下链接 Syntax for multiply

答案 1 :(得分:0)

其他人帮助您解决了$multiply中的语法错误,但更大的问题是您的$前缀字段名称都是错误的。您没有orders级别的文档,而且产品数组的名称为products,而不是product

您还应该将$match过滤器放在管道中,因为在展开之前过滤效率更高。

db.order.aggregate([
    {$match: {"_id": 'O1234'}}, 
    {$unwind: "$products"}, 
    {
        $project: {
            _id:1, 
            prod_name: "$products.prod_name",
            prod_price: "$products.prod_price", 
            order_qty: "$products.order_qty", 
            order_total: {$multiply: ["$products.prod_price", "$products.order_qty"]}
        }
    }
]);