[英]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). 最后,我需要对
Line
和LineStart
的结果进行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.