Golang,MongoDB,有问题使用$ in来查找数组属性中包含一个字符串的所有元素

Kod*_*rty 2 go mongodb

我试图找到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中的条目

任何帮助将不胜感激.

khr*_*hrm 6

您正在使用"$ in"错误.你没有初始化内部地图.你应该像这样使用它:

err := c.Find(bson.M{"friends": bson.M{"$in": arr}}).All(&users)
Run Code Online (Sandbox Code Playgroud)