我试图找到MongoDB集合中包含friends数组中的用户名字符串的所有用户.我正在使用Golang和mgo驱动程序.
type User struct {
...
Friends []string `json: friends bson:"friends,omitempty"`
...
}
...
// username is a string
arr := []string{username}
err := c.Find(bson.M{"friends": {"$in": arr}}).All(&users)
...
Run Code Online (Sandbox Code Playgroud)
我收到此错误:http:panic serving [:: 1]:56358:分配给nil map中的条目
任何帮助将不胜感激.
您正在使用"$ in"错误.你没有初始化内部地图.你应该像这样使用它:
err := c.Find(bson.M{"friends": bson.M{"$in": arr}}).All(&users)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2139 次 |
| 最近记录: |