Perhaps neither. The approach I would suggest is to use a key function
to extract the part of the "dt" value you want to group on. Here's a
sample mongo shell session showing how you could do it:
> db.test.find()
{ "_id" : ObjectId("4e6a1d90a16b32d6fb183f31"), "dt" : "201109082300",
"rows" : 354 }
{ "_id" : ObjectId("4e6a1da2a16b32d6fb183f32"), "dt" : "201109082200",
"rows" : 100 }
{ "_id" : ObjectId("4e6a1dada16b32d6fb183f33"), "dt" : "201109072200",
"rows" : 200 }
{ "_id" : ObjectId("4e6a1db5a16b32d6fb183f34"), "dt" : "201109072300",
"rows" : 300 }
>
> keyf
function (doc) {
return {hour:doc.dt.substring(8, 10)};
}
>
> reduce
function (doc, prev) {
prev.rows += doc.rows;
}
>
> db.test.group({
... $keyf: keyf,
... initial : { rows : 0 },
... $reduce : reduce
... })
[
{
"hour" : "23",
"rows" : 654
},
{
"hour" : "22",
"rows" : 300
}
]
>
If you wanted to group only data for a range of "dt" values then you
would add a condition to the group command. I probably still wouldn't
use regular expressions. For example, all the documents for 2011-09
could be matched using this query:
> db.test.find({ dt : { $gte : "20110901000000", $lt : "2011100100000000" } })
{ "_id" : ObjectId("4e6a1d90a16b32d6fb183f31"), "dt" : "201109082300",
"rows" : 354 }
{ "_id" : ObjectId("4e6a1da2a16b32d6fb183f32"), "dt" : "201109082200",
"rows" : 100 }
{ "_id" : ObjectId("4e6a1dada16b32d6fb183f33"), "dt" : "201109072200",
"rows" : 200 }
{ "_id" : ObjectId("4e6a1db5a16b32d6fb183f34"), "dt" : "201109072300",
"rows" : 300 }