Augustin Popa
Augustin Popa

Reputation: 1543

Filtering & sorting by dates in MongoDB's oplog

I'm trying to filter oplog.rs to view operations that were logged after a certain date. But comparison operators don't seem to work with dates in MongoDB:

db['oplog.rs'].find({ts: {$gte: ISODate("2014-12-15T00:00:00Z")}})

What's confusing is a lot of online sources are saying to do it this way but it is obviously not working. I want to return results where the ts field is at least December 15th or more recent, but I am getting results that are clearly before this period. If I switch $gte with $lte no results at all are displayed, even though there are definitely entries both before and after this specified date.

Also, I can't seem to sort the results either. If I try this:

db['oplog.rs'].find().sort({ts: -1})

I get this:

error: {
    "$err" : "Runner error: Overflow sort stage buffered data usage of 33566005 bytes exceeds internal limit of 33554432 bytes",
    "code" : 17144
}

If I could filter the results to make them more recent, I am hoping it would overcome this sorting error, but I can't even do that with MongoDB's basic operators. How can I filter the results of a find operation by date?

Upvotes: 0

Views: 2262

Answers (1)

Martin
Martin

Reputation: 5332

Comparion operators work just fine with date, but ts is a Timestamp object - not a date.

Your query needs to look something like this:

db['oplog.rs'].find({ts: {$gte: Timestamp(ISODate("2014-12-15T00:00:00Z").getTime(),0)}})

This creates a Timestamp object based on an ISODate and uses it in the query.

Upvotes: 1

Related Questions