gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:26:53 26 4
gpt4 key购买 nike

我试图在 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: 分配给 nil 映射中的条目

如有任何帮助,我们将不胜感激。

最佳答案

您使用的“$in”有误。您没有初始化内部 map 。你应该像这样使用它:

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

关于mongodb - Golang,MongoDB,在使用 $in 查找数组属性中具有一个字符串的所有元素时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37231760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com