dqc2017 2016-05-14 20:52
浏览 889
已采纳

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 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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog