mongodb - How to query the maximum value of updateAt in each userId
阿神
阿神 2017-05-02 09:23:01
0
1
615
{
    userId: OjbectId,
    updatedAt: Date
}

I am a newbie, please give me some advice.

阿神
阿神

闭关修行中......

reply all(1)
Peter_Zhu

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template