douwei2825 2017-08-25 19:32
浏览 70
已采纳

查找所有时间戳少于10秒的mongo db文档

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条回答 默认 最新

  • dongxi3911 2017-08-28 02:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序