I have a document structure like this:
{
"_id" : ObjectId("59d7cd63dc2c91e740afcdb"),
"dateJoined": ISODate("2014-12-28T16:37:17.984Z"),
"dateActivated": ISODate("2015-02-28T16:37:17.984Z"),
"enrolled" : [
{ "month":-10, "enrolled":'00'},
{ "month":-9, "enrolled":'00'},
{ "month":-8, "enrolled":'01'},
//other months
{ "month":8, "enrolled":'11'},
{ "month":9, "enrolled":'11'},
{ "month":10, "enrolled":'00'}
]
}
"month" value in enrolled is relative to dateJoined that range from -X to +X that is pre-populated.
I would like to count number of document with enrolled value of '01' for every sub document that satisfies condition - like "5 months before activating and 2 months after activating". All sub document items must match the condition to count as 1. [Yes, it is possible to enroll before activating :)]
As the month value is not based on dateActivated, I should be able to dynamically calculate this for every document.
I am trying to use MongoDB aggregation framework but not sure how to dynamically.
db.getCollection("enrollments").aggregate(
{ $match:{ //matching condition }},
{ $project: {
enrollments: {
$filter: {
input: "$enrolled",
as: "enrollment",
cond: {
$eq: ['$$enrolled.enroll', '01']
//how can I check for month value here?
}
}
}
}}
)
dateJoinedanddateActivated. Considering that these appear to be "strings" as presented in the question, then any math is basically impossible. You need these to be stored as BSON Date format at the very least."2015-2-28T16:37:17.984Z"is not valid. Should be"2015-02-28T16:37:17.984Z"and would be if you were directly copying data. The exact same millisecond value tells us that you actually edited an existing value rather than directly copied real data.