首页 文章

获取mongodb系列的最新记录

提问于
浏览
64

我想知道一个集合中的最新记录 . 怎么做?

注意:我知道以下命令行查询有效:

1. db.test.find().sort({"idate":-1}).limit(1).forEach(printjson);
2. db.test.find().skip(db.test.count()-1).forEach(printjson)

其中idate添加了时间戳 .

问题是收集是时候收回数据的时间越长,我的'测试'集合真的非常庞大 . 我需要一个具有恒定时间响应的查询 .

如果有任何更好的mongodb命令行查询,请告诉我 .

7 回答

  • 19

    这是对前一个答案的重复,但它更可能适用于不同的mongodb版本 .

    db.collection.find().limit(1).sort({$natural:-1})
    
  • 95

    这将为您提供 collection 的最后一个文件

    db.collectionName.findOne({}, {sort:{$natural:-1}})
    

    $natural:-1 表示与插入记录的顺序相反的顺序 .

    Edit :对于所有downvoters,上面是Mongoose语法,mongo CLI语法是: db.collectionName.find({}).sort({$natural:-1}).limit(1)

  • 23

    我需要一个具有恒定时间响应的查询

    默认情况下,MongoDB中的索引是B-Trees . 搜索B树是O(logN)操作,因此即使 find({_id:...}) 也不会提供恒定时间O(1)响应 .

    如上所述,如果您使用 ObjectId 作为ID,也可以按 _id 排序 . See here for details . 当然,即便如此,直到最后一秒也是如此 .

    你可以诉诸“写两次” . 一次写入主集合并再次写入“最后更新”集合 . 如果没有交易,这将不是完美的,但只有一个项目在“最后更新”的集合中,它总是很快 .

  • 0

    从MongoDB Collection中获取最后一项的另一种方法(不介意示例):

    > db.collection.find().sort({'_id':-1}).limit(1)
    

    Normal Projection

    > db.Sports.find()
    { "_id" : ObjectId("5bfb5f82dea65504b456ab12"), "Type" : "NFL", "Head" : "Patriots Won SuperBowl 2017", "Body" : "Again, the Pats won the Super Bowl." }
    { "_id" : ObjectId("5bfb6011dea65504b456ab13"), "Type" : "World Cup 2018", "Head" : "Brazil Qualified for Round of 16", "Body" : "The Brazilians are happy today, due to the qualification of the Brazilian Team for the Round of 16 for the World Cup 2018." }
    { "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }
    

    Sorted Projection ( _id: reverse order )

    > db.Sports.find().sort({'_id':-1})
    { "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }
    { "_id" : ObjectId("5bfb6011dea65504b456ab13"), "Type" : "World Cup 2018", "Head" : "Brazil Qualified for Round of 16", "Body" : "The Brazilians are happy today, due to the qualification of the Brazilian Team for the Round of 16 for the World Cup 2018." }
    { "_id" : ObjectId("5bfb5f82dea65504b456ab12"), "Type" : "NFL", "Head" : "Patriots Won SuperBowl 2018", "Body" : "Again, the Pats won the Super Bowl" }
    

    sort({'_id':-1}) ,根据 _id s定义所有文档降序的投影 .

    Sorted Projection ( _id: reverse order ) - getting the first (last) document

    > db.Sports.find().sort({'_id':-1}).limit(1)
    { "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }
    
  • 1

    我的解决方案

    db.collection("name of collection").find({}, {limit: 1}).sort({$natural: -1})

  • -1

    如果您在文档中使用自动生成的Mongo Object Ids,则它包含前4个字节的时间戳,使用该字节可以找到插入到集合中的最新文档 . 我知道这是一个老问题,但是如果有人还在这里寻找另一种选择 .

    db.collectionName.aggregate(
    [{$group: {_id: null, latestDocId: { $max: "$_id"}}}, {$project: {_id: 0, latestDocId: 1}}])
    

    上面的查询将为插入到集合中的最新文档提供_id

  • 0

    php7.1 mongoDB:
    $data = $collection->findOne([],['sort' => ['_id' => -1],'projection' => ['_id' => 1]]);

相关问题