MongoDB的:计数匹配数组元素

编程入门 行业动态 更新时间:2024-10-07 04:22:12
本文介绍了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.

我也看到了这个老问题: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的:计数匹配数组元素

本文发布于:2023-10-19 01:45:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1506028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   元素   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!