Agregagate mongo子文档

时间:2017-09-19 21:09:04

标签: node.js mongodb

我有一组文件

_id: 'ObjectId(SomeID),
order_number: Some#,
products: [
    {name: 'SomeName',
    price: SomePrice},
    {name: 'SomeName',
    price: SomePrice},
    {name: 'SomeName',
    price: SomePrice}
]

我正在寻找一种方法来获取包含所有已售产品和订单信息的对象数组,如下所示:

[{
    _id: 'ObjectId(SomeID),
    order_number: Some#,
    name: 'SomeName',
    price: SomePrice}
 },
 {
    _id: 'ObjectId(SomeID),
    order_number: Some#,
    name: 'SomeName',
    price: SomePrice}
 }]

是否可以使用agreggate或其他功能?

1 个答案:

答案 0 :(得分:0)

固定。我用了

collection.aggregate([{$unwind: '$products'}], function(error, result){
    if(error){
        console.log(error);
        return callback(error, 'Error.');
    }
    return callback(null, result);
});
相关问题