MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

mongoDB. leggi, cerca timestamp in base a oplog

Il Timestamp i valori che vedi nell'oplog sono un tipo BSON MongoDB interno. Il primo argomento es nella rappresentazione come una funzione Timestamp(es, ord) è un time_t valore di secondi dall'epoca di Unix. Il secondo è un ordinale che ordina i timestamp entro un secondo. Dovresti essere in grado di interrogare Timestamp s normalmente con $gt , $lt , ecc.:

> 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) }

Per rispondere alle tue domande specifiche (nella mongo shell),

> 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) } })

Non hai specificato un fuso orario:assicurati di fare attenzione e di fare la scelta giusta.

> 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) } })