2
我正在用陶醉和mgo做一個小項目(練習),但是當我構建查詢時,我遇到了搜索功能錯誤。該代碼看起來是這樣的:如何構建mgo查詢?
conditions := make(bson.M, 0)
conditions["status"] = bson.M{"$ne": "delete"}
if item, ok := paramsPost["title"]; ok {
if item[0] != "" {
conditions["title"] = bson.RegEx{Pattern: item[0]}
}
}
if item, ok := paramsPost["from_date"]; ok {
if item[0] != "" {
conditions["publishdate"] = bson.M{}
fromDate, _ := time.Parse("2006-01-02", item[0])
conditions["publishdate"]["$gte"] = fromDate.Unix()
}
}
if item, ok := paramsPost["to_date"]; ok {
if _, ok := conditions["publishdate"]; !ok {
conditions["publishdate"] = bson.M{}
}
if item[0] != "" {
toDate, _ := time.Parse("2006-01-02", item[0])
conditions["publishdate"]["$lte"] = toDate.Unix()
}
}
而且我得到了一些錯誤信息:
invalid operation: conditions["publishdate"]["$gte"] (index of type interface {})
我知道我做了錯事,但我不知道爲什麼,以及如何解決。任何人都可以幫助我?由於
非常感謝〜亞歷克斯,我現在開始工作了。看起來像我以PHP的方式對待golang結構〜哈哈,你真的幫了我很多。 – Vincent