mongoDB.根据oplog读取,搜索时间戳 [英] mongoDB. read, search timestamp based on oplog

查看:368
本文介绍了mongoDB.根据oplog读取,搜索时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> db.oplog.rs.find({},{"ts":1}).sort({$natural: -1})
{ "ts" : Timestamp(1406185666, 1) }  
{ "ts" : Timestamp(1406180043, 1) }  
{ "ts" : Timestamp(1406180033, 1) }  
{ "ts" : Timestamp(1406172831, 1) }  
{ "ts" : Timestamp(1406171938, 1) }  
{ "ts" : Timestamp(1405915291, 1) }  
{ "ts" : Timestamp(1405915131, 1) }  
{ "ts" : Timestamp(1405915019, 1) }  
{ "ts" : Timestamp(1405914592, 1) }  
{ "ts" : Timestamp(1405914581, 1) }  
{ "ts" : Timestamp(1405587944, 1) }  
{ "ts" : Timestamp(1405587920, 1) }  
{ "ts" : Timestamp(1405587468, 1) }  
{ "ts" : Timestamp(1405587432, 1) }  
{ "ts" : Timestamp(1405587393, 1) }  
{ "ts" : Timestamp(1405587294, 1) }  
{ "ts" : Timestamp(1405587074, 1) }  
{ "ts" : Timestamp(1405586117, 1) }  
{ "ts" : Timestamp(1405586069, 1) }  
{ "ts" : Timestamp(1405586054, 1) }  

我在数据库中有来自oplog.rs的一系列时间戳示例.我不知道怎么读

I have a series of timestamp example from oplog.rs in my database. I don't know how to read it

我的问题是:

    假设今天是2014年8月13日8:28.我想选择最近一个小时的所有操作日志
  1. 我想查看2014年8月12日从9:00到15:00的所有操作日志.
  1. let's say that today is 13 aug 2014 8:28. I want to select all the oplog from the last hour
  2. I want to see all of the oplog from 12 aug 2014, from 9:00 to 15:00.

推荐答案

Timestamp 值是内部MongoDB BSON类型.表示形式为函数Timestamp(es, ord)的第一个参数es是自Unix时代以来秒数的time_t值.第二个是序数,它在一秒内订购时间戳.您应该可以使用$gt$lt等正常查询Timestamp:

The Timestamp values you see in the oplog are an internal MongoDB BSON type. The first argument es in the representation as a function Timestamp(es, ord) is a time_t value of seconds since the Unix epoch. The second is an ordinal that orders timestamps within one second. You should be able to query Timestamps normally with $gt, $lt, etc.:

> db.ts.find()
{ "_id" : ObjectId("53eb914ff9b63e0dd3ca1a20"), "ts" : Timestamp(1406185666, 1) }
{ "_id" : ObjectId("53eb915cf9b63e0dd3ca1a21"), "ts" : Timestamp(1405914581, 1) }
{ "_id" : ObjectId("53eb917cf9b63e0dd3ca1a22"), "ts" : Timestamp(1406185666, 2) }

> db.ts.find({ "ts" : { "$gte" : Timestamp(1406185630, 1) } })
{ "_id" : ObjectId("53eb914ff9b63e0dd3ca1a20"), "ts" : Timestamp(1406185666, 1) }
{ "_id" : ObjectId("53eb917cf9b63e0dd3ca1a22"), "ts" : Timestamp(1406185666, 2) }

> db.ts.find({ "ts" : { "$gt" : Timestamp(1406185666, 1) } })
{ "_id" : ObjectId("53eb917cf9b63e0dd3ca1a22"), "ts" : Timestamp(1406185666, 2) }

要回答您的特定问题(在mongo shell中),

To answer your specific questions (in the mongo shell),

假设今天是2014年8月13日8:28.我想选择最近一个小时的所有操作日志

let's say that today is 13 aug 2014 8:28. I want to select all the oplog from the last hour

> var SECS_PER_HOUR = 3600
> var now = Math.floor((new Date().getTime()) / 1000) // seconds since epoch right now
> db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(now, 1), "$gt" : Timestamp(now - SECS_PER_HOUR, 1) } })

我想查看2014年8月12日从9:00到15:00的所有操作日志

I want to see all of the oplog from 12 aug 2014, from 9:00 to 15:00

您未指定时区-请务必注意时区并做出正确的选择.

You didn't specify a timezone - make sure to be careful about that and make the right choice.

> var since = Math.floor(ISODate("2014-08-12T09:00:00.000Z").getTime() / 1000)
> var until = Math.floor(ISODate("2014-08-12T15:00:00.000Z").getTime() / 1000)
> db.oplog.rs.find({ "ts" : { "$lt" : Timestamp(until, 1), "$gt" : Timestamp(since, 1) } })

这篇关于mongoDB.根据oplog读取,搜索时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆