英文:
array of strings sorting as array of Interfaces with mgo in golang
问题
这是我的代码:
type CatMixing struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
CatMix []string `json:"comb"`
}
func main(){
session, err := mgo.Dial("127.0.0.1")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("MixiIng").C("Combination")
var results []bson.M
err5 := c.Find(nil).Limit(10).All(&results)
if err5 == nil {
}
fmt.Println(results)
for _,catm := range results {
fmt.Println(catm)
for _,catm2 := range catm {
fmt.Println(reflect.TypeOf(catm2))
}
}
}
问题是似乎comb
是一个接口数组:
map[_id:ObjectIdHex("590e5cb36aace327da180c89") comb:[60fps]]
bson.ObjectId
[]interface {}
但在MongoDB中,它显示为一个字符串数组:
所以我的映射不起作用...
如果我尝试使用:
var results []CatMixing
我可以得到_id
,但comb
为空。
我不明白为什么它不是一个字符串数组,以及为什么我的映射不起作用。
我使用Python向MongoDB添加了数据:
from pymongo import MongoClient
client = MongoClient()
db = client['MixiIng']
collection = db['Combination']
combination = {}
result = [["60fps"]]
for r in result :
combination = {"comb":r}
collection.insert_one(combination)
所以我不明白为什么comb
不是一个字符串数组,以及如何获取它...
谢谢和问候。
英文:
here is my code :
type CatMixing struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
CatMix []string `json:"comb"`
}
func main(){
session, err := mgo.Dial("127.0.0.1")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("MixiIng").C("Combination")
var results []bson.M
err5 := c.Find(nil).Limit(10).All(&results)
if err5 == nil {
}
fmt.Println(results)
for _,catm := range results {
fmt.Println(catm)
for _,catm2 := range catm {
fmt.Println(reflect.TypeOf(catm2))
}
}
}
the problem is that it seems that comb is an array of interfaces :
map[_id:ObjectIdHex("590e5cb36aace327da180c89") comb:[60fps]]
bson.ObjectId
[]interface {}
but in mongo it shows as an array of string :
So my mapping is not working ...
If i try with :
var results []CatMixing
i have the _id but not comb , comb appears as empty
I don't understand why it's not an array of string and why my mapping is not working.
I've added data to mongodb with python :
from pymongo import MongoClient
client = MongoClient()
db = client['MixiIng']
collection = db['Combination']
combination = {}
result = [["60fps"]]
for r in result :
combination = {"comb":r}
collection.insert_one(combination)
So i don't understand why comb is not an array of string and how to get it...
thanks and regards
答案1
得分: 1
首先,您可以使用[]CatMixing
来更改查询中的results
变量。因为.All(result interface{})
需要一个interface{}
参数,并不意味着您不能传递您的结构体。
请注意,Go中的interface{}
可以保存任何类型,包括您的结构体。
尝试以下代码:
var results []CatMixing
err := c.Find(bson.M{}).Limit(10).All(&results)
if err != nil {
fmt.Fatal(err)
}
fmt.Println(results)
英文:
First you can change the results
variable from the query using your []CatMixing
. Because .All(result interface{})
needs an interace{}
argument doesn't mean you can't pass your struct.
Note that interface{}
in Go can hold any type including your struct.
try this code :
var results [] CatMixing
err := c.Find(bson.M{}).Limit(10).All(&results)
if err != nil {
fmt.Fatal(err)
}
fmt.Println(results)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论