Golang/mgo read bson into map[string]int

640 views
Skip to first unread message

sundar.v...@gmail.com

unread,
Sep 18, 2015, 10:55:10 AM9/18/15
to mgo-users
Hello,

I have the following document in mongo:

{
"_id" : "xxxyyyzzz",
        "e" : {
"426" : 1,
"511" : 2,
"400" : 1,
"c" : 5,
"404" : 1
}
}
}
}


The problem here is, the values "400", "511", etc are dynamic. I have 30 such possible values (they are HTTP Error Codes).

When reading from mgo, using Iter.All() I can pass a structure such as

struct Values {
  T400 int `bson:"400"`
  T401 int `bson:"401"`
....
  T511 int `bson:"511"`
}

This works. However, I’m building a chart from these, and if I have to use this, I have to manually compare all these 30 variables and map them into equivalent strings. 

Is there an easier way?

Would I be able to get them into an map[string]value, such as
{"400":1, "511":2, "426":1, "404":1}

using mgo.

That would be awesome for me.

Thanks
Sundar.




Gustavo Niemeyer

unread,
Sep 18, 2015, 11:05:30 AM9/18/15
to mgo-...@googlegroups.com

Yes, map[string]int works fine, or map[string]value in general.

--
You received this message because you are subscribed to the Google Groups "mgo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

sundar.v...@gmail.com

unread,
Oct 26, 2015, 5:10:28 AM10/26/15
to mgo-users
Thanks. That worked.
Reply all
Reply to author
Forward
0 new messages