【问题标题】:How can i get last inserted record from mongodb with spring boot data?如何使用 Spring Boot 数据从 mongodb 获取最后插入的记录?
【发布时间】:2018-07-02 16:07:24
【问题描述】:

我想知道从 mongodb 获取最后插入的记录与 spring boot 数据? 我没有找到任何确切的解决方案。有没有办法获取最后插入的记录?

【问题讨论】:

    标签: mongodb spring-boot spring-data-mongodb


    【解决方案1】:

    即使它不一定是最后插入的,您也可以通过以下查询获得最新的 ObjectID timestamp

    db.collection.aggregate([
        {$sort: { createdOn: -1 }},
        {$group: { _id: "$itemId", 
            createdOn: {$first: "$createdOn"}
        }}
    ])
    

    【讨论】:

      猜你喜欢
      • 2015-10-12
      • 2014-05-22
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      相关资源
      最近更新 更多