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

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

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中,它显示为一个字符串数组:

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

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

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 :

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

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)

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:

确定