简体   繁体   English

MongoDB:匹配数组元素的计数

[英]MongoDB: Count of matching array elements

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). 我有一个名为“Lines”的集合,具有以下结构(基本上,我有很多包含多个数组的文档,我需要用条件来计算它们的元素)。

    {
        "_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,
            },
            ...
        ]
    },
    ...

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 我需要的是算在这些数组的元素数量符合一定的条件,比如,我想在每一个元素计数SSCCANCELATIONS ,但我只想计算SSCEXPEND与元素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" }, ... } } ) 

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
    },

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). 最后,我需要对LineLineStart的结果进行LineStart ,但我想我已经拥有了其他所有内容(我从日期中得到的日期减去了小时,分钟......)。

So the only thing I need to know is how to count only the array elements I really want. 所以我唯一需要知道的是如何只计算我真正想要的数组元素。

I have read about 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. 但我发现db.collection.group( )方法不适用于分片群集,因此我无法使用它。

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. 我也读过这个老问题: MongoDB:匹配嵌套数组元素的数量或多或少相同,但几乎在五年前得到了解答,当时答案是没有直接的方法去做,所以我问,以防现在有办法。

Method with unwind take a lot of resources. 放松的方法占用了大量资源。 Use directly a project when you can 尽可能直接使用项目

db.c.aggregate([
{
    $project: {
         _id: 1,
         Line: 1,
         LineStart:1,

         SSCEXPEND: {
            $size: {
                $filter: {
                   input: "$SSCEXPEND",
                   as: "e",
                   cond:{ $eq: [ "$$e.Title", 1 ]}
                }
            }
         },
         SSCCANCELATIONS: {
            $size: "$SSCCANCELATIONS"
         },
         SSCVALIDATIONS:{
            $size: {
               $filter: {
                   input: "$SSCVALIDATIONS",
                   as: "v",
                   cond: {$lt: [ "$$v.Result", 10 ]}
                }
            }
         }

      }
}
])

Then simply put your $group to get the sum of all SSCEXPEND, SSCCANCELATIONS, etc... 然后简单地让你的$ group获得所有SSCEXPEND,SSCCANCELATIONS等的总和......

Using mongo aggregation you can find out count, check below aggregation query 使用mongo聚合,您可以找到计数,检查下面的聚合查询

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MongoDB文档中匹配符合条件的数组元素? - Upsert Array Elements matching criteria in a MongoDB document? 更新与 mongodb 中的查询匹配的多个数组元素 - update multiple array elements matching a query in mongodb MongoDB 如何使用 Java 驱动程序计算数组元素 - MongoDB how to count array elements with Java driver MongoDB文档具有用于计数数组元素的字段 - MongoDB document with field to count elements of array 在 MongoDB 中对嵌套数组列表中的匹配元素进行排序和排序 - Sorting and ranking matching elements in a nested array list in MongoDB 如何使用 mongoDB 查询从对象数组中查找匹配元素 - How to find matching elements from an array of objects using mongoDB query MongoDB:使用Java驱动程序在具有给定属性的数组中查找匹配元素 - Mongodb : find matching elements in an array with given attributes using java driver 如何从另一个Array MongoDB中的一个数组计算匹配元素 - How to count matched elements from an Array in another Array MongoDB 轴0上的numpy个计数元素与另一个数组中的值匹配 - numpy count elements across axis 0 matching values from another array 如何获取与 javascript 数组中的谓词匹配的元素计数? - How to get count of elements matching a predicate in a javascript array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM