我正在嘗試查找 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) ...我收到此錯誤:http: panic serving [::1]:56358: assignment to entry in nil map任何幫助將不勝感激。
1 回答

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
您使用“$in”錯誤。您沒有初始化內部地圖。你應該像這樣使用它:
err := c.Find(bson.M{"friends": bson.M{"$in": arr}}).All(&users)
- 1 回答
- 0 關注
- 122 瀏覽
添加回答
舉報
0/150
提交
取消