我正在用 revel 和 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 {})
我知道我做错了什么,但我不知道为什么,以及如何解决。任何人都可以帮助我吗?谢谢