2017-02-22 52 views
1

我创建了一个离子应用程序,我目前正试图从MongoDB中通过Go返回一个数组。这就是MongoDB中的数据。正在返回的空数组

{ 
"_id": { 
    "$oid": "58a86fc7ad0457629d64f569" 
}, 
"name": "ewds", 
"username": "[email protected]", 
"password": "vdseaff", 
"email": "fawfef", 
"usertype": "Coaches", 
"Requests": [ 
    "[email protected]" 
] 
} 

我目前正试图找回请求字段我尝试使用以下代码接收整个文档的方法之一。

//this is the struct being used. 
type (
User struct { 
    Name  string 
    Username string 
    Password string 
    Email string 
    UserType string 
    Requests []string 
} 
) 
results := User{} 
err = u.Find(bson.M{"username": Cname}).One(&results) 

这只返回一个空数组。

{ewds [email protected] vdseaff fawfef Coaches []} 
+1

更新问题以显示'results'的定义。 –

+0

是否有错误? – JimB

+0

没有错误。它的意思是回馈阵列不是一个空阵列 – Racket

回答

1

在您的数据Requests领域有着资本R。该bson库蒙戈文档转换为你的结构类型有这样一段话

https://godoc.org/gopkg.in/mgo.v2/bson#Unmarshal

小写的字段名被用作每个出口领域中的关键,但这种行为可以使用相应的改变字段标签。

那么你的选择是要么添加标签,就可以Requests字段或更改您的数据小写requests使用。如果您选择标签选项,它将类似于

Requests []string `bson:"Requests"` 
+0

非常感谢。 – Racket