MongoDB 2.1聚合框架与名称匹配的数组元素总数 [英] MongoDB 2.1 Aggregate Framework Sum of Array Elements matching a name

查看:75
本文介绍了MongoDB 2.1聚合框架与名称匹配的数组元素总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于在必须与另一个元素匹配的数组中添加一系列数据的最佳方法的问题.我正在尝试使用2.2聚合框架,并且有可能我可以通过一个简单的小组来做到这一点.

This is a question about the best way to add up a series of data in an array where I have to match another element. I'm trying to use the 2.2 Aggregation framework and it's possible I can do this with a simple group.

因此,对于给定的一组文档,我试图获得这样的输出;

So for a given set of documents I'm trying to get an output like this;

{
    "result" : [
            {
                "_id" : null,
                "numberOf": 2,
                "Sales" : 468000,
                "profit" : 246246,
            }
    ],
    "ok" : 1
}

现在,我最初有一个文档列表,其中包含分配给命名属性的值,例如;

Now, I originally had a list of documents, containing values assigned to named properties, eg;

[
{
    _id : 1,
    finance: {
        sales: 234000,
        profit: 123123,
    }
}
,
{
    _id : 2,
    finance: {
        sales: 234000,
        profit: 123123,
    }
}
]

添加起来很容易,但是由于其他原因该结构无法正常工作.例如,可能还有其他列,例如财务",我希望能够在不创建数千个索引的情况下对其进行索引,因此我需要转换为这样的结构;

This was easy enough to add up, but the structure didn't work for other reasons. For instance, there are may other columns like "finance" and I want to be able to index them without creating thousands of indexes, so I need to convert to a structure like this;

[
{
    _id : 1,
    finance: [
        {
            "k": "sales",
            "v": {
                "description":"sales over the year",
                v: 234000,
            }
        },
        {
            "k": "profit",
            "v": {
                "description":"money made from sales",
                v: 123123,
            }
        }
    ]
}
,
{
    _id : 2,
    finance: [
        {
            "k": "sales",
            "v": {
                "description":"sales over the year",
                v: 234000,
            }
        },
        {
            "k": "profit",
            "v": {
                "description": "money made from sales",
                v: 123123,
            }
        }
    ]
}
]

如果愿意,我可以为Finance.k编制索引,但是随后我正在努力构建一个聚合查询,以将与特定键匹配的所有数字相加.这就是我最初使用命名属性的原因,但这确实需要在成千上万个"k"标签的情况下起作用.

I can index finance.k if I want, but then I'm struggling to build an aggregate query to add up all the numbers matching a particular key. This was the reason I originally went for named properties, but this really needs to work in a situation whereby there are thousands of "k" labels.

有人知道如何使用新框架为此建立一个综合查询吗?我已经尝试过了;

Does anyone know how to build an aggregate query for this using the new framework? I've tried this;

db.projects.aggregate([
    {
        $match: {
            // QUERY
            $and: [
                // main query
                {},
            ]
        }
    },
    {
        $group: {
            _id: null,
            "numberOf": { $sum: 1 },
            "sales":    { $sum: "$finance.v.v" },
            "profit":   { $sum: "$finance.v.v" },
        }
    },
])

但我明白了

{
    "errmsg" : "exception: can't convert from BSON type Array to double",
    "code" : 16005,
    "ok" : 0
}

**对于额外的荣誉,我还需要能够在MapReduce查询中做到这一点.

** For extra kudos, I'll need to be able to do this in a MapReduce query as well.

推荐答案

您可以使用汇总框架获取销售利润以及您可能存储的任何其他值在键/值对表示中.

You can use the aggregation framework to get sales and profit and any other value you may be storing in your key/value pair representation.

对于您的示例数据:

var pipeline = [
    {
        "$unwind" : "$finance"
    },
    {
        "$group" : {
            "_id" : "$finance.k",
            "numberOf" : {
                "$sum" : 1
            },
            "total" : {
                "$sum" : "$finance.v.v"
            }
        }
    }
]

R = db.tb.aggregate( pipeline );
printjson(R);
{
        "result" : [
            {
                "_id" : "profit",
                "numberOf" : 2,
                "total" : 246246
            },
            {
                "_id" : "sales",
                "numberOf" : 2,
                "total" : 468000
            }
        ],
        "ok" : 1
}

如果您有其他k/v对,则可以添加仅通过["sales","profit"]中的k个值的匹配项.

If you have additional k/v pairs then you can add a match which only passes through k values in ["sales","profit"].

这篇关于MongoDB 2.1聚合框架与名称匹配的数组元素总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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