查找时间戳小于 10 秒的所有 mongo 个数据库文档
Find all mongo db documents with timestamps less than 10 seconds
我正在尝试获取时间戳少于 10 秒前的所有 mongo 数据库文档。我没有找到任何。我认为这是因为我的时间格式不正确。我没有发现他们从 shell db.mgo.find({timestamp:{$gt: new Date(ISODate().getTime( ) - 86400)}}) 过去 24 小时。
// 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
}
如果我选择其中一个文档 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")
But even if I change to UTC in my func it still not finding any documents
这是因为您的文档中没有字段 timestamp
。您使用的查询语法等同于 ask select all documents where timestamp is greater than T from the collection
。
我假设您的意思是使用从 ObjectId of every documents using getTimestamp()
method. If this is the case, you can utilise mgo/bson function NewObjectIdWithTime() 派生的时间戳值,请参见下面的示例:
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)
上面反推了你的查询,不是用time查询而是用ObjectId查询。否则,您将不得不获取每个文档,将它们的 ObjectId 转换为时间戳并进行比较,这可能效率不高。
或者,根据您的使用情况,您可以使用 $currentDate operator 为每个文档添加一个 timestamp
值
将字段的值设置为当前日期。
我正在尝试获取时间戳少于 10 秒前的所有 mongo 数据库文档。我没有找到任何。我认为这是因为我的时间格式不正确。我没有发现他们从 shell db.mgo.find({timestamp:{$gt: new Date(ISODate().getTime( ) - 86400)}}) 过去 24 小时。
// 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
}
如果我选择其中一个文档 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")
But even if I change to UTC in my func it still not finding any documents
这是因为您的文档中没有字段 timestamp
。您使用的查询语法等同于 ask select all documents where timestamp is greater than T from the collection
。
我假设您的意思是使用从 ObjectId of every documents using getTimestamp()
method. If this is the case, you can utilise mgo/bson function NewObjectIdWithTime() 派生的时间戳值,请参见下面的示例:
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)
上面反推了你的查询,不是用time查询而是用ObjectId查询。否则,您将不得不获取每个文档,将它们的 ObjectId 转换为时间戳并进行比较,这可能效率不高。
或者,根据您的使用情况,您可以使用 $currentDate operator 为每个文档添加一个 timestamp
值
将字段的值设置为当前日期。