I have a collection called "Lines" with the following structure (basically I have a lot of documents containing 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,
},
...
]
},
...
I need to calculate how many elements in these arrays meet certain conditions, for example, I want to count each element from SSCCANCELATIONS, but I only want to count the elements SSCEXPENDusingTitle = 1,
and SSCVALIDATIONS elements with Result < 10
I can get the total number of elements of each array,
db.Lines.aggregate( { $project: { Line : 1, Validations: { $size: "$SSCVALIDATIONS" }, ... } } )
But I need to stabilize the conditions in order 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 Lineand LineStart, but I think I already have everything else (I get the date, counting hours, minutes, ... from the dates that I have).
, , - , .
db.collection.group()
, db.collection.group() , .
:
MongoDB:
, , , , , - .