亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang,MongoDB,使用 $in 在數組屬性中查找具有一個字符串的所有元素時遇到問題

Golang,MongoDB,使用 $in 在數組屬性中查找具有一個字符串的所有元素時遇到問題

Go
哈士奇WWW 2022-01-10 16:33:24
我正在嘗試查找 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)


查看完整回答
反對 回復 2022-01-10
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號