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 aboveerr := 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)}
{[]}