MongoDB 官方 GoLang 驱动程序比较运算符
MongoDB Official GoLang Driver Comparision Operator
我在将项目从 MySQL 迁移到 Mongo 时尝试使用 Go to Learn Mongo 数据库。我遇到的问题之一是了解如何通过官方 Mongo 数据库驱动程序在 Mongo 中执行比较查询。我可以通过 MongoDB Compass 编写相同的查询并且它可以完美地工作,但我不知道如何将其转换为驱动程序中的可行查询。
所以这里是查询
{cached_until: {$lt:1599090092 }}
超级简单,cached_until属性就是一个unix时间戳。当我插入记录时,我将当前的 ts 和缓存到 属性 抓取到它然后执行插入。这是一些数据。
{
"_id": {
"$oid": "5f4ed5a9272d650f55ee822a"
},
"id": {
"$numberLong": "2116190568"
},
"name": "John Gustafson",
"corporation_id": {
"$numberLong": "98522659"
},
"alliance_id": {
"$numberLong": "99003214"
},
"security_status": 2.817923833,
"not_modified_count": {
"$numberLong": "0"
},
"update_priority": {
"$numberLong": "0"
},
"etag": "\"f3a0614b043a556b02d0e53372d0bd0c5bfa5516b9ecf003b5d4fd64\"",
"cached_until": {
"$numberLong": "1599072490"
},
"created_at": {
"$numberLong": "1599002025"
},
"updated_at": {
"$numberLong": "1599002025"
}
}
使用上述查询,上述文档是从 Mongo 返回的,但是如果我使用官方 Mongo DB Go 驱动程序将其转换为 Go,
results, err := app.MongoDB.Collection("characters").Find(ctx, bson.D{bson.E{Key: "cached_until", Value: bson.E{Key: "$lt", Value: time.Now().Add(time.Hour * 24).Unix()}}})
if err != nil {
return err
}
fmt.Println(results.RemainingBatchLength())
输出数字0。在过去一天左右的时间里,我一直在与此作斗争,老实说,我不确定自己做错了什么。这从完整的时间戳开始,但后来我尝试使用 unix 时间戳,当我的比较错误时,我求助于搜索堆栈但找不到任何东西。
我看错了文档。
我设置的过滤器结构不正确,如下:
bson.D{bson.E{Key: "cached_until", Value: bson.E{Key: "$lt", Value: time.Now().Add(time.Hour * 24).Unix()}}}
应该是:
bson.D{bson.E{Key: "cached_until", Value: bson.D{bson.E{Key: "$lt", Value: time.Now().Add(time.Hour * 24).Unix()}}}}
不同之处在于设置值 cached_until
时。我设置的是 Value: bson.E{Key: COlumn, Value: value}
而不是 Value: bson.D{bson.E{Key: Operator, Value: value}}
我在将项目从 MySQL 迁移到 Mongo 时尝试使用 Go to Learn Mongo 数据库。我遇到的问题之一是了解如何通过官方 Mongo 数据库驱动程序在 Mongo 中执行比较查询。我可以通过 MongoDB Compass 编写相同的查询并且它可以完美地工作,但我不知道如何将其转换为驱动程序中的可行查询。
所以这里是查询
{cached_until: {$lt:1599090092 }}
超级简单,cached_until属性就是一个unix时间戳。当我插入记录时,我将当前的 ts 和缓存到 属性 抓取到它然后执行插入。这是一些数据。
{
"_id": {
"$oid": "5f4ed5a9272d650f55ee822a"
},
"id": {
"$numberLong": "2116190568"
},
"name": "John Gustafson",
"corporation_id": {
"$numberLong": "98522659"
},
"alliance_id": {
"$numberLong": "99003214"
},
"security_status": 2.817923833,
"not_modified_count": {
"$numberLong": "0"
},
"update_priority": {
"$numberLong": "0"
},
"etag": "\"f3a0614b043a556b02d0e53372d0bd0c5bfa5516b9ecf003b5d4fd64\"",
"cached_until": {
"$numberLong": "1599072490"
},
"created_at": {
"$numberLong": "1599002025"
},
"updated_at": {
"$numberLong": "1599002025"
}
}
使用上述查询,上述文档是从 Mongo 返回的,但是如果我使用官方 Mongo DB Go 驱动程序将其转换为 Go,
results, err := app.MongoDB.Collection("characters").Find(ctx, bson.D{bson.E{Key: "cached_until", Value: bson.E{Key: "$lt", Value: time.Now().Add(time.Hour * 24).Unix()}}})
if err != nil {
return err
}
fmt.Println(results.RemainingBatchLength())
输出数字0。在过去一天左右的时间里,我一直在与此作斗争,老实说,我不确定自己做错了什么。这从完整的时间戳开始,但后来我尝试使用 unix 时间戳,当我的比较错误时,我求助于搜索堆栈但找不到任何东西。
我看错了文档。
我设置的过滤器结构不正确,如下:
bson.D{bson.E{Key: "cached_until", Value: bson.E{Key: "$lt", Value: time.Now().Add(time.Hour * 24).Unix()}}}
应该是:
bson.D{bson.E{Key: "cached_until", Value: bson.D{bson.E{Key: "$lt", Value: time.Now().Add(time.Hour * 24).Unix()}}}}
不同之处在于设置值 cached_until
时。我设置的是 Value: bson.E{Key: COlumn, Value: value}
而不是 Value: bson.D{bson.E{Key: Operator, Value: value}}