Search code examples
javamongodbparsingbson

How can i parse BSON Timestamp in java


Hello guys i have this Bson Timestamp:

"ts" : { "$timestamp" : { "t" : 1562880169, "i" : 1 } }

How can i get the "t" and "i" values and parse them into variables in java.

I'm getting the Timestamp like this :

 BsonTimestamp timeStamp = (BsonTimestamp) currentDoc.get("ts");

Thank you very much.


Solution

  • The BsonTimestamp object has getInc and getTime and getInc methods to extract those values. http://mongodb.github.io/mongo-java-driver/3.6/javadoc/org/bson/types/BSONTimestamp.html

    To get the time, you would do a timeStamp.getTime() and for 'i' you would do a timeStamp.getInc()