返回空数组

时间:2017-02-22 17:57:05

标签: arrays mongodb go ionic-framework

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

{
"_id": {
    "$oid": "58a86fc7ad0457629d64f569"
},
"name": "ewds",
"username": "affe@dsg.com",
"password": "vdseaff",
"email": "fawfef",
"usertype": "Coaches",
"Requests": [
    "test@t.com"
]
}

我目前正在尝试返回“请求”字段,其中一种方法是尝试使用以下代码接收整个文档。

//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 affe@dsg.com vdseaff fawfef Coaches []}

1 个答案:

答案 0 :(得分:1)

在您的数据中,Requests字段的首字母为R。将mongo文档转换为结构类型的bson库可以说

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

  

小写字段名称用作每个导出字段的键,但可以使用相应的字段标记更改此行为。

因此,您可以选择在Requests字段中添加标记,也可以将数据更改为使用小写requests。如果您选择标签选项,那就像

Requests []string `bson:"Requests"`