{
userId: OjbectId,
updatedAt: Date
}
I am a newbie, please give me some advice.
天蓬老师2017-05-02 09:25:01
If you know how to write it in SQL, the same principle applies to MongoDB. This is a very common grouped aggregation query. The answers are below, but I still hope you can try to search for the answers online and exercise your ability to solve problems independently.
db.collection.aggregate([
{$group: {_id: "$userId", maxUpdatedAt: {$max: "$updatedAt"}}}
]);
The answer can be easily obtained using aggregation’s $max operator.