Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoDB. read, search timestamp based on oplog

Tags:

mongodb

> db.oplog.rs.find({},{"ts":1}).sort({$natural: -1})
{ "ts" : Timestamp(1406185666, 1) }  
{ "ts" : Timestamp(1406180043, 1) }  
{ "ts" : Timestamp(1406180033, 1) }  
{ "ts" : Timestamp(1406172831, 1) }  
{ "ts" : Timestamp(1406171938, 1) }  
{ "ts" : Timestamp(1405915291, 1) }  
{ "ts" : Timestamp(1405915131, 1) }  
{ "ts" : Timestamp(1405915019, 1) }  
{ "ts" : Timestamp(1405914592, 1) }  
{ "ts" : Timestamp(1405914581, 1) }  
{ "ts" : Timestamp(1405587944, 1) }  
{ "ts" : Timestamp(1405587920, 1) }  
{ "ts" : Timestamp(1405587468, 1) }  
{ "ts" : Timestamp(1405587432, 1) }  
{ "ts" : Timestamp(1405587393, 1) }  
{ "ts" : Timestamp(1405587294, 1) }  
{ "ts" : Timestamp(1405587074, 1) }  
{ "ts" : Timestamp(1405586117, 1) }  
{ "ts" : Timestamp(1405586069, 1) }  
{ "ts" : Timestamp(1405586054, 1) }  

I have a series of timestamp example from oplog.rs in my database. I don't know how to read it

My questions are:

  1. let's say that today is 13 aug 2014 8:28. I want to select all the oplog from the last hour
  2. I want to see all of the oplog from 12 aug 2014, from 9:00 to 15:00.
like image 444
Rifi331 Avatar asked Aug 13 '14 01:08

Rifi331


1 Answers

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) } })
like image 88
wdberkeley Avatar answered Oct 14 '22 02:10

wdberkeley