Reputation: 41
I am trying to query timestamp field of mongo oplog collection using java.
Below is the code.
BSONTimestamp timestamp1 = new BSONTimestamp(1499172935, 1);
BasicDBObject query1 = new BasicDBObject("ts", new BasicDBObject("$gt", timestamp1) );
DBCursor cursor = dbCollection.find(query1);
When I run above piece of code, it returns nothing.
Below is the converted query.
{ "ts" : { "$gt" : { "$ts" : 1499172935 , "$inc" : 1}} }
I executed the same query using robomongo and it also returns nothing.
db.getCollection('oplog.rs').find({ "ts" : { "$gt" : { "$ts" : 1499172935 , "$inc" : 1}} })
But when I changed the query to use Timestamp and executed it, it returns list of oplog records. Below is the working mongo query.
db.getCollection('oplog.rs').find({ "ts" : { "$gt" : Timestamp(1499172935 , 1)} })
How can I get the above query using java? or Is there any other way I can query oplog timestamp field using java?
Upvotes: 2
Views: 818
Reputation: 174
You can use the type 'BsonTimeStamp' to build your filter.
BsonTimestamp lastReadTimestamp = new BsonTimestamp(time, inc);
Bson filter = new Document("$gt", lastReadTimestamp);
dbCollection.find(new Document("ts", filter));
Upvotes: 1