I want to find the count of maximum consecutive records based on one particular field.
My db.people
collection after finding sort based on field is:
> db.people.find().sort({ updated_at: 1})
{ "_id" : 1, "name" : "aaa", "flag" : true, "updated_at" : ISODate("2014-02-07T08:42:48.688Z") }
{ "_id" : 2, "name" : "bbb", "flag" : false, "updated_at" : ISODate("2014-02-07T08:43:10Z") }
{ "_id" : 3, "name" : "ccc", "flag" : true, "updated_at" : ISODate("2014-02-07T08:43:40.660Z") }
{ "_id" : 4, "name" : "ddd", "flag" : true, "updated_at" : ISODate("2014-02-07T08:43:51.567Z") }
{ "_id" : 6, "name" : "fff", "flag" : false, "updated_at" : ISODate("2014-02-07T08:44:23.713Z") }
{ "_id" : 7, "name" : "ggg", "flag" : true, "updated_at" : ISODate("2014-02-07T08:44:44.639Z") }
{ "_id" : 8, "name" : "hhh", "flag" : true, "updated_at" : ISODate("2014-02-07T08:44:51.415Z") }
{ "_id" : 5, "name" : "eee", "flag" : true, "updated_at" : ISODate("2014-02-07T08:55:24.917Z") }
In above records, there are two places where flag
attribute value comes true
in consecutive ways. i.e
record with _id 3 - record with _id 4 (2 consecutive records)
and
record with _id 7 - record with _id 8 - record with _id 5 (3 consecutive records)
However, I want the maximum consecutive number from mongo query search. i.e 3
.
Is it possible to get such result?
I googled it and found a little similar solution of using Map-Reduce
here https://stackoverflow.com/a/7408639/1120530.
I am new to mongodb and couldn't able to understand the map-reduce
documentation and specially how to apply it in above scenario.
You can do this mapReduce operation.
First the mapper:
Which keeps a running count of the total times that the
true
value is seen in flag. If that count is more than 1 then we emit the the value, also containing the document_id
. Another counter which is used for the key is incremented when the flag isfalse
, in order to have a grouping "key" for the matches.Then the reducer:
Simply pushes the
_id
values onto a result array along with the totalCount.Then run:
So with the
mapper
andreducer
functions, we then define the global variables used in "scope" and pass in the "sort" that was required onupdated_at
dates. Which gives the result:Of course you could just skip the
totalCount
variable and just use the array length, which would be the same. But since you want to use that counter anyway it's just added in. But that's the principle.So yes, this was a problem suited to mapReduce, and now you have an example.