mongoDB. read, search timestamp based on oplog

后端 未结 1 402
没有蜡笔的小新
没有蜡笔的小新 2021-01-05 17:28
> db.oplog.rs.find({},{\"ts\":1}).sort({$natural: -1})
{ \"ts\" : Timestamp(1406185666, 1) }  
{ \"ts\" : Timestamp(1406180043, 1) }  
{ \"ts\" : Timestamp(140618         


        
相关标签:
1条回答
  • 2021-01-05 18:10

    The Timestamp values you see in the oplog are an internal MongoDB BSON type. The first argument es in the representation as a function Timestamp(es, ord) is a time_t value of seconds since the Unix epoch. The second is an ordinal that orders timestamps within one second. You should be able to query Timestamps normally with $gt, $lt, etc.:

    > db.ts.find()
    { "_id" : ObjectId("53eb914ff9b63e0dd3ca1a20"), "ts" : Timestamp(1406185666, 1) }
    { "_id" : ObjectId("53eb915cf9b63e0dd3ca1a21"), "ts" : Timestamp(1405914581, 1) }
    { "_id" : ObjectId("53eb917cf9b63e0dd3ca1a22"), "ts" : Timestamp(1406185666, 2) }
    
    > db.ts.find({ "ts" : { "$gte" : Timestamp(1406185630, 1) } })
    { "_id" : ObjectId("53eb914ff9b63e0dd3ca1a20"), "ts" : Timestamp(1406185666, 1) }
    { "_id" : ObjectId("53eb917cf9b63e0dd3ca1a22"), "ts" : Timestamp(1406185666, 2) }
    
    > db.ts.find({ "ts" : { "$gt" : Timestamp(1406185666, 1) } })
    { "_id" : ObjectId("53eb917cf9b63e0dd3ca1a22"), "ts" : Timestamp(1406185666, 2) }
    

    To answer your specific questions (in the mongo shell),

    let's say that today is 13 aug 2014 8:28. I want to select all the oplog from the last hour

    > var SECS_PER_HOUR = 3600
    > var now = Math.floor((new Date().getTime()) / 1000) // seconds since epoch right now
    > db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(now, 1), "$gt" : Timestamp(now - SECS_PER_HOUR, 1) } })
    

    I want to see all of the oplog from 12 aug 2014, from 9:00 to 15:00

    You didn't specify a timezone - make sure to be careful about that and make the right choice.

    > var since = Math.floor(ISODate("2014-08-12T09:00:00.000Z").getTime() / 1000)
    > var until = Math.floor(ISODate("2014-08-12T15:00:00.000Z").getTime() / 1000)
    > db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(until, 1), "$gt" : Timestamp(since, 1) } })
    
    0 讨论(0)
提交回复
热议问题