2016-05-14 31 views
1

我試圖在包含朋友數組中的用戶名字符串的MongoDB集合中查找所有用戶。我正在使用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:恐慌服[:: 1]:56358:在無地圖

任何幫助分配條目將不勝感激。

回答

3

您正在使用「$ in」錯誤。你沒有初始化內部地圖。你應該這樣使用它:

err := c.Find(bson.M{"friends": bson.M{"$in": arr}}).All(&users) 
+0

我試過了,現在我得到了。 http:恐慌服務[:: 1]:59541:分配到無記錄地址 –

+1

不要緊,這個問題在HTTP響應中處於困境。你是正確的解決了這個問題。感謝您的幫助! –

相關問題