douraoyw194498 2016-05-12 13:42
浏览 171

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

The mongo shell output

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

The function takes depID as input which I verified is exactly what it should be i.e. 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
}

the out put of result comes {[]}

i have tried the following queries other that above
err := coll.Find(bson.M{"id": depID})One(&result)
err := coll.Find(nil).One(&result)
the result is same.

everything is fine with db connection and other things as all the other functions of the same file using the same variable coll works

More Info Also I have tried this

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

Mongo LOG

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)}
{[]}
  • 写回答

3条回答 默认 最新

  • dongzhuo1958 2016-05-12 18:54
    关注

    I think the issue may be that your UserResp struct has private members.

    //Instead of
     users []string `json:"users" bson:"users"`
    
    // Try
     Users []string `json:"users" bson:"users"`
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配