I have mongodb collection like this:
{
"_id" : ObjectId("51111618b419e37023f5070c"),
"checkCode" : "statusCode",
"checkDate" : ISODate("2013-02-05T18:24:24.927Z"),
"domain" : ObjectId("511102021ffae419c2363c33"),
"pageUrl" : "/index.html",
"checkResult" : "404"
}
The collection can has many records with same domain, pageUrl, checkCode but different checkResult (or the same) and different checkDate (date of the check).
So I need to select only last by date checkResult by pageUrl and checkCode in given domain. What is the best and faster way to do this (there could be lots of records by domain and check results) ? Should I use group or map/reduce? Distinct works too slowly…
You could use the aggregation framework here like so:
Using the
$matchand$sortwith the$firstoperator allows for index usage which could make a performant group for at least a substantial number of records.