如何从地图中检索价值 - 去朗? [英] How to retrieve value from map - go lang?

查看:114
本文介绍了如何从地图中检索价值 - 去朗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与 mgo 一起使用MongoDB和Go。我有以下代码:

  func查找(collectionName,dbName字符串,查询接口{})(result [] interface {} ,错误){
collection:= session.DB(dbName).C(collectionName)
err = collection.Find(query).All(& result)
return result,err

$ b 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)
}

$ / code>

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



我需要获取 _id Find 中返回的映射值中创建c>和 transId 。我怎么能这样做?

解决方案

我只是猜测,但如果你只是想检索所有 Transaction 文件并打印它们 - 这里是如何:

鉴于你有一个 struct ,它代表您的集合的文档例如:

 类型事务结构{
Id bson.ObjectId`bson:_ id`
TransactionId字符串`bson:transId`
}

您可以获取全部使用MongoDB驱动程序(mgo)的文档:

  var transactions [] Transaction 
err = c.Find(bson .M {})。所有(&交易)
//处理err
用于索引,transaction:= range transactions {
fmt.Printf(%d:%+ v \ n,index,transaction)
}

添加(通用解决方案)



好的,在您提供了一些更多的洞察之后,这可能是没有您的通用解决方案唱一个结构。尝试编入BSON文档 bson.M (未测试):

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


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?

解决方案

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)
  }
}    

这篇关于如何从地图中检索价值 - 去朗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆