Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

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
like image 414
iDev_周晶 Avatar asked Feb 21 '23 16:02

iDev_周晶


2 Answers

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.

like image 165
Lycha Avatar answered Feb 23 '23 05:02

Lycha


You're simply using ".test" while you shouldn't be. The following works:

db.oplog.rs.find( {'ts.t': 1335200998000 } );
like image 38
Derick Avatar answered Feb 23 '23 06:02

Derick