如何在文档中获得数组的$ sum?

时间:2014-02-16 09:18:26

标签: mongodb nosql-aggregation

例如,我有以下文件:

{
    "_id" : ObjectId("52ffddd3c69e40174c046d67"),
    "works" : [ 
        {
            "service_id" : ObjectId("52ffd576c69e40174c046d64"),
            "price" : 150,
            "count" : 3,
            "items" : [ 
                {
                    "price" : 5,
                    "amount" : 10
                }, 
                {
                    "price" : 7,
                    "amount" : 3
                }
            ]
        }, 
        {
            "service_id" : ObjectId("52ffd56fc69e40174c046d63"),
            "price" : 100,
            "count" : 2
        }
    ]
}

凡service_id是指某些外部文件(我知道,不推荐),价格是工作价格,数量是工作执行的次数,物品是材料的数组,用于全部有效。 我需要得到所有作品的总价。我尝试了未完成的代码:

db.service_cards.aggregate(
    { $match: {_id: ObjectId("52ffddd3c69e40174c046d67") } },
    { $unwind: "$works" },
    { $project: {
        "works.service_id": 1,
        "works.price": 1,
        "works.count": 1,
        "works.items": { 
            "$cond": [ 
                { $eq: [ "$works.items", []] },
                [{
                    "price": 0,
                    "amount": 0
                }],
                "$works.items"
            ],
        }
    } },
    { $project: {
        "works.service_id": 1,
        "works.price": 1,
        "works.count": 1,
        "works.items": { 
            "$ifNull": [ 
                "$works.items",
                [{
                    "price": 0,
                    "amount": 0
                }]
            ],
        }
    } },
    { $unwind: "$works.items" },
    { $project: { 
        "service_id": "$works.service_id",
        "price": "$works.price",
        "count": "$works.count",
        "items_cost": { $multiply: ["$works.items.price", "$works.items.amount"] },
    } },
    { $group: { 
        _id: "$service_id", 
        "sum_items": { $sum: "$items_cost" },
        "price": { $avg: "$price" },
        "count": { $avg: "$count" }
    } }
)

它实际上并不对数组求和 - 它展开作品和项目,然后使用$ group来对未展开的项目进行求和。但后来我记得 - 可能有两件作品具有相同的service_id,数量和价格,在$ unwind和$ group之后,我将丢失其中一件,将两件物品放在一起。

有没有办法在没有$ unwind的情况下在文档中获取数组$ sum?

修改 我希望收到一份没有物品的作品清单,但总成本为:

{
    "_id" : ObjectId("52ffddd3c69e40174c046d67"),
    "works" : [ 
        {
            "service_id" : ObjectId("52ffd576c69e40174c046d64"),
            "price" : 150,
            "count" : 3,
            "items_cost" : 71
        }, 
        {
            "service_id" : ObjectId("52ffd56fc69e40174c046d63"),
            "price" : 100,
            "count" : 2,
            "items_cost": 0
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

我认为,这不是更好的解决方案,但它很短并且做你想做的事情:

db.service_cards.find().forEach(function (document) {
    var works = document.works || [];
    var result = [];
    works.forEach(function (work) {
        var items = work.items || [];
        var itemsCost = 0;
        items.forEach(function (item) {
            itemsCost += item.price * item.amount;
        });
        var resultObj = {
            service_id: work.service_id,
            price: work.price,
            count: work.count,
            items_cost: itemsCost
        };
        result.push(resultObj);
    });
    print({
        _id: document._id,
        works: result
    });
})