我希望找到特定字段的最大连续记录计数。
在对字段进行排序后,我的db.people
集合如下:
> 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") }
在上述记录中,有两个地方连续出现
flag
属性值为true
。即record with _id 3 - record with _id 4 (2 consecutive records)
并且
record with _id 7 - record with _id 8 - record with _id 5 (3 consecutive records)
然而,我想要从Mongo查询搜索中获取最大连续数字,即
3
。是否可能获得这样的结果?我谷歌了一下,并在这里找到了一个类似的解决方案,使用Map-Reduce
,链接如下:https://stackoverflow.com/a/7408639/1120530。我是Mongodb的新手,无法理解map-reduce
文档,特别是如何将其应用于上述情况。