gpt4 book ai didi

mongodb - mgo.v2 Find().Select().One() 返回空白

转载 作者:IT王子 更新时间:2023-10-29 02:23:13 25 4
gpt4 key购买 nike

mongo shell 输出

> db.departments.find()
{ "_id" : ObjectId("5733b051e444917f9d273ce6"), "id" : "5733b05157659a11a4589102", "brandid" : "1", "name" : "first", "managerid" : "0", "users" : [ "1", "2", "3", "7" ] }

该函数将 depID 作为输入,我验证了它应该是什么,即 5733b05157659a11a4589102

func GetUsers(depID string) (*department.UsersResp, error) {
if !bson.IsObjectIdHex(depID) {
return nil, fmt.Errorf("%s is not a ObjectId value", depID)
}
type UserResp struct {
users []string
}
var result *UserResp
err := coll.Find(bson.M{"id": depID}).Select(bson.M{"users": 1}).One(&result)

if err != nil {
fmt.Println(err, result)
return nil, err
}
fmt.Println("result:",result)
//ignore return type and value the above print is what i have doubt about
return result, nil
}

结果输出为{[]}

除了上述以外,我已经尝试了以下查询
err := coll.Find(bson.M{"id": depID})One(&result)
err := coll.Find(nil).One(&result)
结果是一样的。

数据库连接和其他一切都很好,因为使用相同变量 coll 的同一文件的所有其他功能都有效

更多信息我也试过这个

type UserResp struct {
users []string `json:"users" bson:"users"`
}

Mongo 日志

MGO: cluster.go:590: Cluster has 1 known masters and 0 known slaves.
MGO: socket.go:391: Socket 0xc8201340e0 to 127.0.0.1:27017: serializing op: &mgo.queryOp{collection:"departments.departments", query:bson.M{"id":"5733b05157659a11a4589102"}, skip:0, limit:-1, selector:bson.M{"users":1}, flags:0x4, replyFunc:(mgo.replyFunc)(0x58eef0), mode:1, options:mgo.queryWrapper{Query:interface {}(nil), OrderBy:interface {}(nil), Hint:interface {}(nil), Explain:false, Snapshot:false, ReadPreference:bson.D(nil), MaxScan:0, MaxTimeMS:0, Comment:""}, hasOptions:false, serverTags:[]bson.D(nil)}
MGO: socket.go:521: Socket 0xc8201340e0 to 127.0.0.1:27017: sending 1 op(s) (106 bytes)
MGO: socket.go:311: Socket 0xc8201340e0 to 127.0.0.1:27017: updated write deadline to 1m0s ahead (2016-05-12 10:18:17.811427695 -0400 EDT)
MGO: socket.go:311: Socket 0xc8201340e0 to 127.0.0.1:27017: updated read deadline to 1m0s ahead (2016-05-12 10:18:17.811468447 -0400 EDT)
MGO: socket.go:563: Socket 0xc8201340e0 to 127.0.0.1:27017: got reply (106 bytes)
MGO: socket.go:622: Socket 0xc8201340e0 to 127.0.0.1:27017: received document: bson.M{"_id":"W3\xb0Q\xe4D\x91\u007f\x9d'<\xe6", "users":[]interface {}{"1", "2", "3", "7"}}
MGO: session.go:3062: Query 0xc82012c000 document unmarshaled: &db.UserResp{users:[]string(nil)}
{[]}

最佳答案

我认为问题可能在于您的 UserResp 结构具有私有(private)成员。

//Instead of
users []string `json:"users" bson:"users"`

// Try
Users []string `json:"users" bson:"users"`

关于mongodb - mgo.v2 Find().Select().One() 返回空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37188760/

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