MongoDB的:计数匹配数组元素 [英] MongoDB: Count of matching array elements

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

问题描述

我有一个集合所谓的线,其结构如下(基本上,我有很多的包含多个阵列的文件和我需要的条件来计算它们的元素)。

I have a collection called "Lines" with the following structure (basically, I have a lot of documents that contain several arrays and I need to count their elements with conditions).

    {
        "_id" : "201503110040020021",
        "Line" : "1", // several documents may have this Line value
        "LineStart" : ISODate("2015-03-11T06:49:35.000Z"),
        "SSCEXPEND" : [ 
            {
                "Secuence" : 10,
                "Title" : 1,
            }, 
            {
                "Secuence" : 183,
                "Title" : 613,
            }, 
            ...
        ],
        "SSCCANCELATIONS" : [ 
            {
                "Secuence" : 34,
                "Title" : 113,
            }, 
            {
                "Secuence" : 96,
                "Title" : 2,
            }, 
            ... 
        ],
        "SSCVALIDATIONS" : [ 
            {
                "Secuence" : 12,
                "Result" : 1
            }, 
            {
                "Secuence" : 15,
                "Result" : 1,
            },
            {
                "Secuence" : 18,
                "Result" : 20,
            },
            ...
        ]
    },
    ...

我需要的是计算有多少元素,这些阵列符合一定的条件,例如,
我想算
SSCCANCELATIONS 的每一个元素,
但我只想用计数 SSCEXPEND 元素标题= 1,
并与结果&LT SSCVALIDATIONS元素; 10

What I need is to count how many elements in those arrays match certain conditions, for instance, I want to count every element in SSCCANCELATIONS, but I only want to count SSCEXPEND elements with Title = 1, and SSCVALIDATIONS elements with Result < 10

我可以得到每个数组的元素总数

I can get the total number of elements of every array, with

db.Lines.aggregate( { $project: { Line : 1, Validations: { $size: "$SSCVALIDATIONS" }, ... } } ) 

但我需要的条件16:25惟有,要达到这样的:

But I need to stablish conditions, to get something like:

    {
        "_id" : "201503110040020021",
        "Line" : "1",
        "LineStart" : ISODate("2015-03-11T06:49:35.000Z"),
        "SSCEXPEND" : 15,
        "SSCCANCELATIONS" : 10,
        "SSCVALIDATIONS" : 462
    },

在最后,我将需要集团 LineStart 的结果,但我想我已经拥有一切(我得到的日期减去小时,分钟,...从日期我有)。

In the end, I will need to group the result for Line and LineStart, but I think I already have everything else (I get the date substracting hours, minutes,... from the dates I have).

所以,我需要知道的唯一事情是怎么算只有数组元素我真正想要的。

So the only thing I need to know is how to count only the array elements I really want.

我读过关于 db.collection.group()

但我发现 db.collection.group()方法不能与分片集群的工作,所以我不能使用它。

but I found the db.collection.group() method does not work with sharded clusters, so I can't use it.

我也看到了这个老问题:
<一href=\"http://stackoverflow.com/questions/3168043/mongodb-count-of-matching-nested-array-elements\">MongoDB:匹配嵌套的数组元素的计数
这或多或少是一样的,但它几乎是五年前回答的时候,得到的答案是,有这样做没有直接的方法,所以我的情况下,问现在有一种方式。

I have also read this old question: MongoDB: Count of matching nested array elements which is more or less the same, but it was answered almost five years ago and at the time, the answer was that there's no direct way to do it, so I am asking in case there is a way now.

推荐答案

使用蒙戈聚集你可以找到数,检查下面聚集查询

Using mongo aggregation you can find out count, check below aggregation query

db.Lines.aggregate([
{
    "$unwind": "$SSCEXPEND"
},
{
    "$unwind": "$SSCVALIDATIONS"
},
{
    "$match": {
        "$and": [
            {
                "SSCEXPEND.Title": 1
            },
            {
                "SSCVALIDATIONS.Result": {
                    "$gt": 10
                }
            }
        ]
    }
},
{
    "$group": {
        "_id": "$_id",
        "SSCEXPEND": {
            "$addToSet": "$SSCEXPEND"
        },
        "SSCVALIDATIONS": {
            "$addToSet": "$SSCVALIDATIONS"
        },
        "SSCCANCELATIONS": {
            "$first": "$SSCCANCELATIONS"
        }
    }
},
{
    "$project": {
        "SSCEXPENDCOUNT": {
            "$size": "$SSCEXPEND"
        },
        "SSCVALIDATIONSCOUNT": {
            "$size": "$SSCVALIDATIONS"
        },
        "SSCCANCELATIONSCOUNT": {
            "$size": "$SSCCANCELATIONS"
        }
    }
}
]).pretty()

这篇关于MongoDB的:计数匹配数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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