How to retrieve value from map – go lang?

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

How to retrieve value from map - go lang?

问题

我正在使用mgo来在Go中使用MongoDB。我有以下的代码:

func Find(collectionName, dbName string, query interface{}) (result []interface{}, err error) {
    collection := session.DB(dbName).C(collectionName)
    err = collection.Find(query).All(&result)
    return result, err
}

func GetTransactionID() (id interface{}, err error) {
    query := bson.M{}
    transId, err := Find("transactionId", dbName, query)
    for key, value := range transId {
        fmt.Println("k:", key, "v:", value)
    }
}

输出结果为:k: 0 v: map[_id:ObjectIdHex("536887c199b6d0510964c35b") transId:A000000000]

我需要从Find返回的切片中的映射值中获取_idtransId的值。我该如何做到这一点?

英文:

I am working with mgo to use MongoDB with Go. I have the following code:

func Find(collectionName, dbName string, query interface{}) (result []interface{}, err error) {
	collection := session.DB(dbName).C(collectionName)
	err = collection.Find(query).All(&result)
	return result, err
}

func GetTransactionID() (id interface{}, err error) {
	query := bson.M{}
	transId, err := Find("transactionId", dbName, query)
	for key, value := range transId {
		fmt.Println("k:", key, "v:", value)
	}
}

Output: k: 0 v: map[_id:ObjectIdHex("536887c199b6d0510964c35b") transId:A000000000]

I need to get the values of _id and transId from the map value returned in the slice from Find. How can I do that?

答案1

得分: 9

我只是猜测,但是如果你只想检索并打印所有的Transaction文档,可以按照以下方式进行操作:

假设你有一个表示集合中文档结构的struct,例如:

type Transaction struct {
  Id            bson.ObjectId `bson:"_id"`
  TransactionId string        `bson:"transId"`
}

你可以使用MongoDB驱动程序(mgo)获取所有文档:

var transactions []Transaction
err = c.Find(bson.M{}).All(&transactions)
// 处理错误
for index, transaction := range transactions {
  fmt.Printf("%d: %+v\n", index, transaction)
}

补充(通用解决方案)

好的,在你提供了更多信息之后,这可能是一个不使用struct的通用解决方案。尝试将其编组为BSON文档bson.M(未经测试):

var data []bson.M
err := c.Find(bson.M{}).All(&data)
// 处理错误
for _, doc := range data {
  for key, value := range doc {
    fmt.Println(key, value)
  }
}
英文:

I'm just guessing but in case you just want to retrieve all Transaction documents and print them - here is how:

Given you have a struct representing the structure of the documents of your collection e.g.:

type Transaction struct {
  Id            bson.ObjectId `bson:"_id"`
  TransactionId string        `bson:"transId"`
}

You can obtain all the documents using the MongoDB driver (mgo):

var transactions []Transaction
err = c.Find(bson.M{}).All(&transactions)
// handle err
for index, transaction := range transactions {
  fmt.Printf("%d: %+v\n", index, transaction)
}

Addition (generic solution)

OK, after you provided some more insight this might be a generic solution without using a struct. Try to marshall into a BSON document bson.M (not tested):

var data []bson.M
err := c.Find(bson.M{}).All(&data)
// handle err 
for _, doc := range data {
  for key, value := range doc {
    fmt.Println(key, value)
  }
}    

huangapple
  • 本文由 发表于 2014年5月6日 15:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/23488704.html
匿名

发表评论

匿名网友

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

确定