使用mgo在golang中将字符串数组按照接口数组进行排序。

huangapple go评论210阅读模式
英文:

array of strings sorting as array of Interfaces with mgo in golang

问题

这是我的代码:

  1. type CatMixing struct {
  2. Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
  3. CatMix []string `json:"comb"`
  4. }
  5. func main(){
  6. session, err := mgo.Dial("127.0.0.1")
  7. if err != nil {
  8. panic(err)
  9. }
  10. defer session.Close()
  11. session.SetMode(mgo.Monotonic, true)
  12. c := session.DB("MixiIng").C("Combination")
  13. var results []bson.M
  14. err5 := c.Find(nil).Limit(10).All(&results)
  15. if err5 == nil {
  16. }
  17. fmt.Println(results)
  18. for _,catm := range results {
  19. fmt.Println(catm)
  20. for _,catm2 := range catm {
  21. fmt.Println(reflect.TypeOf(catm2))
  22. }
  23. }
  24. }

问题是似乎comb是一个接口数组:

  1. map[_id:ObjectIdHex("590e5cb36aace327da180c89") comb:[60fps]]
  2. bson.ObjectId
  3. []interface {}

但在MongoDB中,它显示为一个字符串数组:

使用mgo在golang中将字符串数组按照接口数组进行排序。

所以我的映射不起作用...
如果我尝试使用:

  1. var results []CatMixing

我可以得到_id,但comb为空。

我不明白为什么它不是一个字符串数组,以及为什么我的映射不起作用。

我使用Python向MongoDB添加了数据:

  1. from pymongo import MongoClient
  2. client = MongoClient()
  3. db = client['MixiIng']
  4. collection = db['Combination']
  5. combination = {}
  6. result = [["60fps"]]
  7. for r in result :
  8. combination = {"comb":r}
  9. collection.insert_one(combination)

所以我不明白为什么comb不是一个字符串数组,以及如何获取它...

谢谢和问候。

英文:

here is my code :

  1. type CatMixing struct {
  2. Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
  3. CatMix []string `json:"comb"`
  4. }
  5. func main(){
  6. session, err := mgo.Dial("127.0.0.1")
  7. if err != nil {
  8. panic(err)
  9. }
  10. defer session.Close()
  11. session.SetMode(mgo.Monotonic, true)
  12. c := session.DB("MixiIng").C("Combination")
  13. var results []bson.M
  14. err5 := c.Find(nil).Limit(10).All(&results)
  15. if err5 == nil {
  16. }
  17. fmt.Println(results)
  18. for _,catm := range results {
  19. fmt.Println(catm)
  20. for _,catm2 := range catm {
  21. fmt.Println(reflect.TypeOf(catm2))
  22. }
  23. }
  24. }

the problem is that it seems that comb is an array of interfaces :

  1. map[_id:ObjectIdHex("590e5cb36aace327da180c89") comb:[60fps]]
  2. bson.ObjectId
  3. []interface {}

but in mongo it shows as an array of string :

使用mgo在golang中将字符串数组按照接口数组进行排序。

So my mapping is not working ...
If i try with :

  1. 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 :

  1. from pymongo import MongoClient
  2. client = MongoClient()
  3. db = client['MixiIng']
  4. collection = db['Combination']
  5. combination = {}
  6. result = [["60fps"]]
  7. for r in result :
  8. combination = {"comb":r}
  9. 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{}可以保存任何类型,包括您的结构体。

尝试以下代码:

  1. var results []CatMixing
  2. err := c.Find(bson.M{}).Limit(10).All(&results)
  3. if err != nil {
  4. fmt.Fatal(err)
  5. }
  6. 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 :

  1. var results [] CatMixing
  2. err := c.Find(bson.M{}).Limit(10).All(&results)
  3. if err != nil {
  4. fmt.Fatal(err)
  5. }
  6. fmt.Println(results)

huangapple
  • 本文由 发表于 2017年5月7日 07:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/43826463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定