如何构建mgo查询?

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

How to build mgo queries?

问题

我正在使用revel和mgo做一个小项目(练习),但是在构建查询时,搜索功能出了一些问题。代码看起来像这样:

  1. conditions := make(bson.M, 0)
  2. conditions["status"] = bson.M{"$ne": "delete"}
  3. if item, ok := paramsPost["title"]; ok {
  4. if item[0] != "" {
  5. conditions["title"] = bson.RegEx{Pattern: item[0]}
  6. }
  7. }
  8. if item, ok := paramsPost["from_date"]; ok {
  9. if item[0] != "" {
  10. conditions["publishdate"] = bson.M{}
  11. fromDate, _ := time.Parse("2006-01-02", item[0])
  12. conditions["publishdate"]["$gte"] = fromDate.Unix()
  13. }
  14. }
  15. if item, ok := paramsPost["to_date"]; ok {
  16. if _, ok := conditions["publishdate"]; !ok {
  17. conditions["publishdate"] = bson.M{}
  18. }
  19. if item[0] != "" {
  20. toDate, _ := time.Parse("2006-01-02", item[0])
  21. conditions["publishdate"]["$lte"] = toDate.Unix()
  22. }
  23. }

然后我得到了一些错误信息:

  1. invalid operation: conditions["publishdate"]["$gte"] (index of type interface {})

我知道我做错了什么,但是我不知道为什么,也不知道如何解决。有人可以帮助我吗?谢谢。

英文:

I'm doing a tiny project(practice) with revel and mgo, but I got something wrong with search function when I build the queries. The code seems like this:

  1. conditions := make(bson.M, 0)
  2. conditions["status"] = bson.M{"$ne": "delete"}
  3. if item, ok := paramsPost["title"]; ok {
  4. if item[0] != "" {
  5. conditions["title"] = bson.RegEx{Pattern: item[0]}
  6. }
  7. }
  8. if item, ok := paramsPost["from_date"]; ok {
  9. if item[0] != "" {
  10. conditions["publishdate"] = bson.M{}
  11. fromDate, _ := time.Parse("2006-01-02", item[0])
  12. conditions["publishdate"]["$gte"] = fromDate.Unix()
  13. }
  14. }
  15. if item, ok := paramsPost["to_date"]; ok {
  16. if _, ok := conditions["publishdate"]; !ok {
  17. conditions["publishdate"] = bson.M{}
  18. }
  19. if item[0] != "" {
  20. toDate, _ := time.Parse("2006-01-02", item[0])
  21. conditions["publishdate"]["$lte"] = toDate.Unix()
  22. }
  23. }

And I got some error info:

  1. invalid operation: conditions["publishdate"]["$gte"] (index of type interface {})

I know I make something wrong, but I don't know why, and how to resolve. Anybody can help me? Thanks

答案1

得分: 4

bson.M 是一个 map[string]interface{} 类型的变量(http://godoc.org/labix.org/v2/mgo/bson#M)。

所以,在以下代码中:

  1. conditions["publishdate"]["$gte"] = fromDate.Unix()

当在 map 中查找 publishdate 时,你需要将 interface{} 类型断言为 bson.M 类型。

相反,你可以重构代码,改为以下形式:

  1. publishdate := bson.M{}
  2. // ... 在这里添加你的逻辑
  3. conditions["publishdate"] = publishDate

这样可以避免不必要的 map 查找和类型断言。

英文:

bson.M is a map[string]interface{} (http://godoc.org/labix.org/v2/mgo/bson#M)

So, in

  1. conditions["publishdate"]["$gte"] = fromDate.Unix()

You need to do a type assertion from interface{} to bson.M when looking up publishdate in the map.

Instead, you could refactor the code to something like

  1. publishdate:= bson.M{}
  2. // ... your logic goes here
  3. conditions["publishdate"] = publishDate

to save on unnecessary map lookups and type assertions.

huangapple
  • 本文由 发表于 2014年2月16日 01:03:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/21800777.html
匿名

发表评论

匿名网友

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

确定