我正在嘗試查找 MongoDB 集合中的所有用戶,該集合中包含 Friends 數(shù)組中的用戶名字符串。我正在將 Golang 與 mgo 驅(qū)動(dòng)程序一起使用。 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) ...我收到此錯(cuò)誤:http: panic serving [::1]:56358: assignment to entry in nil map任何幫助將不勝感激。
1 回答

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