mgo.v2 Find()。选择()。一个()返回空白

时间:2016-05-12 13:42:32

标签: mongodb go mgo

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工作的同一文件的所有其他功能都可以正常使用db连接和其他东西

更多信息 我也试过这个

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 个答案:

答案 0 :(得分:1)

我认为问题可能是您的UserResp结构有私有成员。

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

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

答案 1 :(得分:0)

猜测很疯狂,但也许可以试试

var result UserResp

而不是使用指针。

同样提及 Walt ,您也应该导出Users字段。

答案 2 :(得分:-1)

  1. users未导出,应为Users
  2. 您不需要指针指针,我个人只需使用var result UserResp
  3. 以下更正:

    type UserResp struct {
        Users []string 
    }
    var result UserResp
    err := coll.Find(bson.M{"id": depID}).Select(bson.M{"users": 1}).One(&result)
    

    如果这些不是问题,那么我在处理BSON和数组/切片时遇到了很多问题。您可以尝试将Users作为一个接口片段,或者只是将整个输出写入bson.M映射(map[string]interface{})并从那里开始。以下是我将如何做到这一点:

    var result bson.M
    err := coll.Find(bson.M{"id": depID}).Select(bson.M{"users": 1}).One(&result)
    

    您可以使用类型断言访问基础数据:

    result["users"].([]string)
    

    如果这不起作用,只需检查基础数据以调试返回的类型:

    fmt.Printf("%+v", result)
    

    无论哪种方式,一旦你有像Go这样的数据。调试会容易得多。