select 函数无法只获取我想要的数组
select function does't work to only get the array i want
我有这样的文档
{
"_id": {
"$oid": "570bc73da8ebd9005dd54de3"
},
"title": "dota",
"imgurl": "asd.com",
"description": "",
"hints": [
{
"date": "2016-04-26 22:50:12.6069011 +0430 IRDT",
"description": "narinin"
},
{
"date": "2016-04-26 22:50:12.6069011 +0430 IRDT",
"description": "doros shod"
}
]
}
我执行的脚本是
hints := hints{}
err := db.C("games").Find(bson.M{"title": game}).Select(bson.M{"hints": 0}).One(&hints)
我的 2 个结构是
type Game struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Title string `json:"title"`
Imgurl string `json:"imgurl"`
Description string `json:"desc"`
Hints []*hint `bson:"hints", json:"hints"`
}
type hint struct {
Description string `json:"desc"`
Date time.Time `json:"date"`
}
当我使用脚本时,我得到的只是 none 感觉日期字符串 甚至不在文档中
我怎样才能从游戏中获得提示片段
您一直在使用 Game
结构来接收结果,即使只有 hints
列也是如此。另外,您的 select 查询应该是 .Select(bson.M{"hints": 1})
.
我修改了你的代码,并在我的本地试过了,这个可以用。
game := Game{}
err = db.C("games").Find(bson.M{"title": "dota"})
.Select(bson.M{"hints": 1}).One(&game)
if err != nil {
panic(err)
}
for _, hint := range game.Hints {
fmt.Printf("%#v\n", *hint)
}
game
的所有属性均为空,Hints
除外。
已编辑 1
要获取 hints
上的前 10 行,最简单的方法是播放切片,但这很糟糕,因为它需要先获取所有行。
for _, hint := range game.Hints[:10] { // 10 rows
fmt.Printf("%#v\n", *hint)
}
另一种解决方案(更好)是在 .Select()
查询中使用 $slice
。
selector := bson.M{"hints": bson.M{"$slice": 10}} // 10 rows
err = db.C("so").Find(bson.M{"title": "dota"})
.Select(selector).One(&game)
已编辑 2
在 $slice
上使用 []int{skip, limit}
,以支持跳过和限制。
selector := bson.M{"hints": bson.M{"$slice": []int{0, 10}}}
我有这样的文档
{
"_id": {
"$oid": "570bc73da8ebd9005dd54de3"
},
"title": "dota",
"imgurl": "asd.com",
"description": "",
"hints": [
{
"date": "2016-04-26 22:50:12.6069011 +0430 IRDT",
"description": "narinin"
},
{
"date": "2016-04-26 22:50:12.6069011 +0430 IRDT",
"description": "doros shod"
}
]
}
我执行的脚本是
hints := hints{}
err := db.C("games").Find(bson.M{"title": game}).Select(bson.M{"hints": 0}).One(&hints)
我的 2 个结构是
type Game struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Title string `json:"title"`
Imgurl string `json:"imgurl"`
Description string `json:"desc"`
Hints []*hint `bson:"hints", json:"hints"`
}
type hint struct {
Description string `json:"desc"`
Date time.Time `json:"date"`
}
当我使用脚本时,我得到的只是 none 感觉日期字符串 甚至不在文档中 我怎样才能从游戏中获得提示片段
您一直在使用 Game
结构来接收结果,即使只有 hints
列也是如此。另外,您的 select 查询应该是 .Select(bson.M{"hints": 1})
.
我修改了你的代码,并在我的本地试过了,这个可以用。
game := Game{}
err = db.C("games").Find(bson.M{"title": "dota"})
.Select(bson.M{"hints": 1}).One(&game)
if err != nil {
panic(err)
}
for _, hint := range game.Hints {
fmt.Printf("%#v\n", *hint)
}
game
的所有属性均为空,Hints
除外。
已编辑 1
要获取 hints
上的前 10 行,最简单的方法是播放切片,但这很糟糕,因为它需要先获取所有行。
for _, hint := range game.Hints[:10] { // 10 rows
fmt.Printf("%#v\n", *hint)
}
另一种解决方案(更好)是在 .Select()
查询中使用 $slice
。
selector := bson.M{"hints": bson.M{"$slice": 10}} // 10 rows
err = db.C("so").Find(bson.M{"title": "dota"})
.Select(selector).One(&game)
已编辑 2
在 $slice
上使用 []int{skip, limit}
,以支持跳过和限制。
selector := bson.M{"hints": bson.M{"$slice": []int{0, 10}}}