How to convert bson to json

4,221 views
Skip to first unread message

Alex Luya

unread,
Aug 6, 2012, 4:33:05 AM8/6/12
to mgo-...@googlegroups.com
If I have a method like this:

func ExcuteQuery(collection string, query interface{}) []interface{} {
ses := ensureMongoConnection("ExcuteQuery")
defer ses.Close()

         var result []interface
err := ses.DB(databaseName).C(collection).Find(query).All(&result)
if err != nil {
panic(err)
}

    return result
}

It will return a bson slice,I tried these:

     bsonSlice.([]MyType) and bsonSlice[0].(MyType)

both get errors,so if I don't want to change method signature like this:

     func ExcuteQuery(collection string, query interface{},result []interface{}) {...}

How can I convert this returned bson slice to MyType{}?




Nguyên Nguyễn Văn Cao

unread,
Aug 6, 2012, 9:10:20 AM8/6/12
to mgo-...@googlegroups.com
BSON is some thing like JSON, so need u function to parse it:
http://go.pkgdoc.org/labix.org/v2/mgo/bson#Unmarshal

2012/8/6 Alex Luya <alexand...@gmail.com>:
--
Nguyễn Văn Cao Nguyên
/**
* Contact : +84+935704595
* Blog : http://nguyen.hocvui.net
*/

Jordan Orelli

unread,
Aug 6, 2012, 2:57:47 PM8/6/12
to mgo-...@googlegroups.com
make the result variable take the type that you want to unmarshal to.  In this case, it should be defined:

    var result []MyType

now, MyType should have a definition that uses struct tags to hint how the unmarshalling should work.  Let's say it's a Person:

    type Person struct {
        FirstName string `bson:"first_name"`
        LastName string `bson:"last_name"`
    }

then you could just say this:

    var people []Person
    someCollection.Find( ... ).All(&people)



Your title question asked something different, though; how to just convert from bson to json.  That's much simpler; just unmarshal to interface{} and then marshal from interface{}

    var v interface{}
    err := someCollection.Find( ... ).All(&v)
    // check err
    rawjson, err := json.Marshal(v)
    // check err


if you unmarshal to interface{}, you wind up with an opaque datastructure.  That's often not very useful because it gives you no compile-time safety, but if you're only going to pass it to things that are built to handle arbitrary data structures using reflection (as is the case with the json.Marshal function), then it's very useful.

Alexander Luya

unread,
Aug 11, 2012, 1:04:29 AM8/11/12
to mgo-...@googlegroups.com

What I really want to do are: hide mongdb connections stuffs(creation,clone..close) and error handling stuff like this:
func ExcuteQuery(collection string, query interface{}) []byte {


 ses := ensureMongoConnection("ExcuteQuery")
 defer ses.Close()

 var result []interface{}
 err := ses.DB(databaseName).C(collection).Find(query).All(&result)
 if err != nil {
  panic(err)
 }

 // check err
 rawjson, err := json.Marshal(result)
 // return result


 if err != nil {
  panic(err)
 }

 return rawjson
}

And test caller will like this:

func (s *MySuite) TestGetModel(c *C) {
 res := mgd.ExcuteQuery(collection, nil)
//************************************here something get printed out***********************************
 fmt.Println(res)
 var list []MySuite
 json.Unmarshal(res, list)
//************************************but why here is length of list 0?***********************************
 c.Check(list[0].Value, Equals, value)

Reply all
Reply to author
Forward
0 new messages