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

本人菜鸟,求教一下.

阿神
阿神

闭关修行中......

répondre à tous(1)
Peter_Zhu

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

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

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

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!