mongodb - 怎么查询每个userId中updateAt最大的值
阿神
阿神 2017-05-02 09:23:01
0
1
498
{
    userId: OjbectId,
    updatedAt: Date
}

本人菜鸟,求教一下.

阿神
阿神

闭关修行中......

reply all(1)
Peter_Zhu

如果用SQL知道应该怎么写,用MongoDB也是一样的原理。这是一个很普通的分组聚合查询。答案在下面,但还是希望你可以自己尝试去网上自己搜索一下答案,锻炼独立解决问题的能力。

db.collection.aggregate([
  {$group: {_id: "$userId", maxUpdatedAt: {$max: "$updatedAt"}}}
]);

使用aggregation的$max运算符可以很容易得到答案。

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!