Reputation: 832
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...
Upvotes: 0
Views: 152
Reputation: 43884
You could use the aggregation framework here like so:
db.col.aggregate([
{$match:{domain: ObjectId("511102021ffae419c2363c33")}},
{$sort:{checkDate:-1}},
{$group: {_id: {pageUrl: '$pageUrl', checkCode: '$checkCode'}, checkResult: {$first: '$checkResult'}}}
])
Using the $match
and $sort
with the $first
operator allows for index usage which could make a performant group for at least a substantial number of records.
Upvotes: 1