查找所有时间戳小于10秒的MongoDB文档。

huangapple go评论75阅读模式
英文:

Find all mongo db documents with timestamps less than 10 seconds

问题

我正在尝试获取所有时间戳早于10秒前的MongoDB文档,但是没有找到任何文档。我认为这是因为我的时间格式不正确。我在过去24小时内使用MongoDB的shell查询时没有找到它们,查询语句为db.mgo.find({timestamp:{$gt: new Date(ISODate().getTime() - 86400)}})

// 查找最近10分钟的文档
func FindLast(session *mgo.Session, db, collection string) ([]Syslog, error) {
var results []Syslog
t := time.Now().Add(-10 * time.Second)
c := session.DB(db).C(collection)
err := c.Find(
bson.M{
"timestamp": bson.M{
"$gt": t,
},
}).All(&results)
return results, err
}

如果我选择其中一个文档ObjectId("...").getTimestamp(),它显示的是ISODate("2017-08-25T19:14:54Z"),比我的时间提前了大约4小时,所以它是UTC时间。但是,即使我在我的函数中将时间改为UTC,仍然找不到任何文档。

t := time.Now().UTC().Add(-time.Duration(10)*time.Minute).Format("2006-01-02 15:04:05")

英文:

I am trying to get all mongo db documents with timestamps less than 10 seconds ago. I am not finding any. I think this is because my time format is not correct. I am not finding them querying mongo db from shell db.mgo.find({timestamp:{$gt: new Date(ISODate().getTime() - 86400)}}) for last 24h.

// FindLast 10min
func FindLast(session *mgo.Session, db, collection string) ([]Syslog, error) {
  var results []Syslog
  t := time.Now().Add(-10 * time.Second)
  c := session.DB(db).C(collection)
  err := c.Find(
    bson.M{
        "timestamp": bson.M{
            "$gt": t,         
        },
  }).All(&results)
  return results, err
}

If I pick one of document ObjectId("...").getTimestamp() it shows ISODate("2017-08-25T19:14:54Z") which is about 4h ahead of me so it is UTC. But even if I change to UTC in my func it still not finding any documents

t := time.Now().UTC().Add(-time.Duration(10)*time.Minute).Format("2006-01-02 15:04:05")

答案1

得分: 1

即使我在我的函数中更改为UTC,它仍然找不到任何文档。

这是因为您的文档中没有timestamp字段。您使用的查询语法相当于询问“选择所有时间戳大于T的文档”。

我猜您的意思是使用每个文档的ObjectId派生的时间戳值,使用getTimestamp()方法。如果是这样的话,您可以利用mgo/bson包中的NewObjectIdWithTime()函数,示例如下:

currentTime := time.Now()
queryTime := currentTime.Add(-10 * time.Second)

// 生成一个带有指定时间戳的虚拟ObjectId用于查询
var oidtime = bson.NewObjectIdWithTime(queryTime)
query := bson.M{"_id": bson.M{"$gt": oidtime}}
err := collection.Find(query).All(&documents)

上述代码反转了查询方式,不再使用时间来查询,而是利用ObjectId来查询。否则,您将不得不获取每个文档,将其ObjectId转换为时间戳并进行比较,这可能不太高效。

另外,根据您的用例,您可以使用$currentDate操作符为每个文档添加一个timestamp值,以将字段的值设置为当前日期。

英文:

> But even if I change to UTC in my func it still not finding any documents

This is because there is no field timestamp within your document. The query syntax that you're using is equivalent to ask select all documents where timestamp is greater than T from the collection.

I assume that what you're meaning to do is to use the timestamp value derived from the ObjectId of every documents using getTimestamp() method. If this is the case, you can utilise mgo/bson function NewObjectIdWithTime() see example as below:

currentTime := time.Now()
queryTime := currentTime.Add(-10 * time.Second)

// Generate a dummy ObjectId with a specified timestamp for querying
var oidtime = bson.NewObjectIdWithTime(queryTime)
query := bson.M{"_id": bson.M{"$gt": oidtime}}
err := collection.Find(query).All(&documents)

The above reversed your querying, instead of using time to query we utilises ObjectId to query. Otherwise you would have to fetch every document, convert their ObjectId's to timestamp and compare, which may not be as efficient.

Alternatively, depending on your use case you can add a timestamp value for each document using $currentDate operator
to set the value of a field to the current date.

huangapple
  • 本文由 发表于 2017年8月26日 03:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/45888313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定