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"`
}
蒙哥日志
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),
蝴蝶刀刀
BIG阳
慕妹3242003
相关分类