question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decode "timestamp" from oplog.timestamp

See original GitHub issue

On the wiki it says: The exact format of this file depends on MongoDB’s toplogy. For a single replica set, the format is:

["oplog name", timestamp]

An example would be:

["Collection(Database(MongoClient('localhost', 27017), u'local'), u'oplog.rs')", 6185588281674039941]

What format is this timestamp in, and how do you convert say, 6185588281674039941, to a normal date?

Thanks.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
howardkhlcommented, Sep 15, 2015

Okay, found a solution for this.

util.py has a method long_to_bson_ts, use Python to run the method directly:

python -c 'import util; print util.long_to_bson_ts(<val>)'

For example:

python -c 'import util; print util.long_to_bson_ts(6185145118358503425)'

And you will get a Timestamp object back.

Timestamp(1440091319, 1)

In python, use datetime to convert Timestamp to human readable form:

python
>>> from datetime import datetime
>>> str(datetime.fromtimestamp(1440091319))

It will return:

'2015-08-20 10:21:59'

For comparison purpose, this command will return current system time:

>>> str(datetime.now())
0reactions
howardkhlcommented, Jan 13, 2022

Update, I’ve tried the following on my mac terminal and no dependencies issue. Convert from Epoch time to bson format (oplog) and vice versa.

https://atharva-inamdar.medium.com/understanding-mongodb-oplog-249f3996f528

  1. Install pymongo pip install pymongo
  2. In Python3 terminal python3
  3. Copy paste:
from bson import Timestamp
from datetime import datetime
def ts_to_int(ts):
  return (ts.time << 32) + ts.inc
def int_to_ts(t):
  return Timestamp(t>>32, t & (2**32)-1)
  1. Try it out:
>>> ts=Timestamp(datetime.utcnow(), 1)
>>> print(ts_to_int(ts))
7052809609722986497
>>> ts=Timestamp(1642100404, 1)
>>> print(ts_to_int(ts))
7052767531928387585
Read more comments on GitHub >

github_iconTop Results From Across the Web

how to Convert Oplog Timestamp into ISO Date-time
When i fetched the timestamp of operation i got is as ". ts" : Timestamp(1412767541, 1). How can i convert into ISO date-time...
Read more >
MongoDB : Convert Oplog timestamp into ISO date
Most easy way to figure out the recent changes (any DB any collection) in mongo Database is to query to Oplog with sorting...
Read more >
Mongo 3.4 oplog timestamp to epoch - Stack Overflow
I am trying to convert oplog ts field to unix epoch time. oplog document { "ts" : Timestamp(6454921106666029, 1), "t" : NumberLong(2), ...
Read more >
[MONGOSH-1029] Cannot use timestamp from oplog for filtering
I'm trying to prepare a query that will use the `clusterTime` for this operation but with no luck. The clusterTime is a BSON...
Read more >
Tools for representing MongoDB internal Timestamps
Tools for representing MongoDB internal Timestamps. ... Create a new Timestamp . This class is only for use with the MongoDB opLog. If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found