dqc2017
2016-05-14 20:52 阅读 432

Golang,MongoDB,使用$ in在数组属性中查找带有一个字符串的所有元素时遇到问题

I am trying to find all users in a MongoDB collection that contains a username string in the friends array. I am using Golang with the mgo driver.

   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)
    ...

I get this error: http: panic serving [::1]:56358: assignment to entry in nil map

Any help would be greatly appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doumi1852 doumi1852 2016-05-15 00:55

    You are using "$in" wrong. You aren't initialising inner map. You are supposed to use it like this:

    err := c.Find(bson.M{"friends": bson.M{"$in": arr}}).All(&users)
    
    点赞 评论 复制链接分享

相关推荐