子文档中所有键的总值 [英] Total values from all keys in subdocument

查看:58
本文介绍了子文档中所有键的总值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MongoDB集合,其中包含如下文档:

I have a MongoDB collection with documents that look like:

{
    '_id': 'doc1',
    'store_A': {'apples': 50, 'oranges':20},
    'store_B': {'oranges': 15}
}
{
    '_id': 'doc2',
    'store_A': {'oranges':10},
    'store_B': {'apples': 15}
}

如何编写一个聚合命令,以便在不枚举所有允许的水果种类的情况下,为我提供集合中所有文档中每个商店的水果总数?

结果应如下所示:

{
    '_id': 'Result',
    'store_A_total': {'apples': 50, 'oranges': 30},
    'store_B_total': {'apples': 15, 'oranges': 15}
}

此查询有效,但必须明确指定所有水果类型:

This query works, but all the fruit types must be specified explicitly:

db.collection.aggregate(
{'$group': {'_id': 'Result',
    'store_A_apples': {'$sum': '$Store_A.apples'},
    'store_A_oranges': {'$sum': '$store_A.oranges'},
    'store_B_apples': {'$sum': '$store_B.apples'},
    'store_B_oranges': {'$sum': '$store_B.oranges'}
}},
{'$project': {
    'store_A': {'apples': '$store_A_apples','oranges': '$store_A_oranges'},
    'store_B': {'apples': '$store_B_apples','oranges': '$store_B_oranges'}
}})

是否有更好的方法来组织这些文档以简化此类查询?

推荐答案

mongodb聚合框架中没有一种方法可以处理 将文档内部的密钥作为您可以检查或处理的数据.一种 解决方法是在此处打开您用作键的内容(例如,水果类型 并存储名称)为这样的值:

There isn't a way in the mongodb aggregation framework for treating a key inside of a document as data you can examine or manipulate. A workaround is to turn what you're using as keys here (e.g. fruit type and store name) into values like this:

{
    "_id" : "doc1",
    "stores":[
        {
            // store name is a value
            "name":"store_A",
            "inventory": [
            {
                // so is fruit type
                "type" : "apple",
                "count" : 50
            },
            {
                "type" : "orange",
                "count" : 20
            }
            ]
        },
        {
            "name": "store_B",
            "inventory": [
            {
                "type" : "orange",
                "count" : 15
            }
            ]
        }
    ]
}

这使您可以更轻松地使用这些数据进行汇总:

This allows you to work with these data more easily in aggregation:

db.coll.aggregate([
    // split documents by store name
    {$unwind:"$stores"},
    // split documents further by fruit type
    {$unwind:"$stores.inventory"},
    // group documents together by store/fruit type, count quantities of fruit
    {$group:{"_id":{"store":"$stores.name", "fruit":"$stores.inventory.type"},
             "count":{$sum:"$stores.inventory.count"}}},
    // reformat the data to look more like your specification
    {$project:{
        "store":"$_id.store",
        "fruit":"$_id.fruit",
        "_id":0,
        "count":1}}])

输出如下:

{
    "result" : [
        {
            "count" : 15,
            "store" : "store_B",
            "fruit" : "apple"
        },
        {
            "count" : 15,
            "store" : "store_B",
            "fruit" : "orange"
        },
        {
            "count" : 30,
            "store" : "store_A",
            "fruit" : "orange"
        },
        {
            "count" : 50,
            "store" : "store_A",
            "fruit" : "apple"
        }
    ],
    "ok" : 1
}

这篇关于子文档中所有键的总值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆