MongoDB Cannot find entry by specifing ts.t(ts is a Timestamp type)

mongodb

Solution

I believe the ts field is actually a Timestamp field, the console just tries to simplify it for you (which does make it very misleading). You can do the query like this and it should work:

db.oplog.rs.find({ ts: Timestamp(1335200998000, 540)});

You can use $gte and $lte as normal:

db.oplog.rs.find({ ts: {$gte: Timestamp(1335100998000, 1)}});
db.oplog.rs.find({ ts: {$lte: Timestamp(1335900998000, 1)}});

The second argument is an incremental ordinal for operations within a given second.

Problem

Cannot find entry by specifing ts.t(ts is a Timestamp type) Digging the oplog, I want to figure out how many operations there are in a second. Cannot find entry by specifing a timestamp field, ok with other fields. $ In mongo shell: ``` > db.oplog.rs.findOne() { "ts" : { "t" : 1335200998000, "i" : 540 }, "h" : NumberLong("4405509386688070776"), "op" : "i", "ns" : "new_insert", "o" : { "_id" : ObjectId("4f958fad55ba26db6a000a8b"), "username" : "go9090", "message" : "hello, test.", } } > db.oplog.rs.find().count() 419583 > db.oplog.rs.test.find({"ts.t":1335200998000}).count() 0 > db.oplog.rs.test.find({"ts.t":/^1335200998/}).count() 0 > db.oplog.rs.test.find({ts:{ "t" : 1335200998000, "i" : 540 }}).count() 0 ```

Original source