FAQ
I want to use mongodb in golang and wrote an sample application:

package main

import (
     "fmt"
     "labix.org/v2/mgo"
     "labix.org/v2/mgo/bson"
     "os"
)

type Session struct {
     Id bson.ObjectId `bson:"_id"`
     Data map[string]interface{} `bson:"data"`
}

func (rcv *Session) SetBSON(raw bson.Raw) error {
     return raw.Unmarshal(rcv)
}


type Authen struct {
     Name string `bson:"name"`
     Email string `bson:"email"`
}

func main() {
     uri := "mongodb://localhost/"
     if uri == "" {
         fmt.Println("no connection string provided")
         os.Exit(1)
     }

     sess, err := mgo.Dial(uri)
     if err != nil {
         fmt.Printf("Can't connect to mongo, go error %v\n", err)
         os.Exit(1)
     }
     defer sess.Close()

     sess.SetSafe(&mgo.Safe{})
     collection := sess.DB("test").C("sess")

     a := &Authen{Name: "Cormier", Email: "cormier@example.com"}
     s := &Session{}
     s.Id = bson.NewObjectId()
     s.Data = make(map[string]interface{})
     s.Data["logged"] = a

     err = collection.Insert(s)
     if err != nil {
         fmt.Printf("Can't insert document: %v\n", err)
         os.Exit(1)
     }

     c := &Session{}
     c.Id = bson.NewObjectId()
     c.Data = make(map[string]interface{})

     err = sess.DB("test").C("sess").Find(bson.M{}).One(c)
     if err != nil {
         fmt.Printf("got an error finding a doc %v\n")
         os.Exit(1)
     }

}



Insert into mongodb works like a charm, but to unmarshal back to reference
I've got following panic:

runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

runtime stack:
runtime.throw(0x6d84d9)
         c:/go/src/runtime/panic.go:491 +0xad
runtime.newstack()
         c:/go/src/runtime/stack.c:784 +0x5ef
runtime.morestack()
         c:/go/src/runtime/asm_amd64.s:324 +0x86



What am I doing wrong?

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

Search Discussions

  • Carlos Romero Brox at Mar 8, 2015 at 6:44 pm
    I'd say
    func (rcv *Session) SetBSON(raw bson.Raw) error {
         return raw.Unmarshal(rcv)
    }

    causes infinite recursion. I don`t think it is necessary at all. Removing
    that method from your code, it should work.



    El viernes, 6 de marzo de 2015, 16:14:38 (UTC+1), Eric Koston escribió:

    I want to use mongodb in golang and wrote an sample application:

    package main

    import (
    "fmt"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
    "os"
    )

    type Session struct {
    Id bson.ObjectId `bson:"_id"`
    Data map[string]interface{} `bson:"data"`
    }

    func (rcv *Session) SetBSON(raw bson.Raw) error {
    return raw.Unmarshal(rcv)
    }


    type Authen struct {
    Name string `bson:"name"`
    Email string `bson:"email"`
    }

    func main() {
    uri := "mongodb://localhost/"
    if uri == "" {
    fmt.Println("no connection string provided")
    os.Exit(1)
    }

    sess, err := mgo.Dial(uri)
    if err != nil {
    fmt.Printf("Can't connect to mongo, go error %v\n", err)
    os.Exit(1)
    }
    defer sess.Close()

    sess.SetSafe(&mgo.Safe{})
    collection := sess.DB("test").C("sess")

    a := &Authen{Name: "Cormier", Email: "cor...@example.com <javascript:>
    "}
    s := &Session{}
    s.Id = bson.NewObjectId()
    s.Data = make(map[string]interface{})
    s.Data["logged"] = a

    err = collection.Insert(s)
    if err != nil {
    fmt.Printf("Can't insert document: %v\n", err)
    os.Exit(1)
    }

    c := &Session{}
    c.Id = bson.NewObjectId()
    c.Data = make(map[string]interface{})

    err = sess.DB("test").C("sess").Find(bson.M{}).One(c)
    if err != nil {
    fmt.Printf("got an error finding a doc %v\n")
    os.Exit(1)
    }

    }



    Insert into mongodb works like a charm, but to unmarshal back to reference
    I've got following panic:

    runtime: goroutine stack exceeds 1000000000-byte limit
    fatal error: stack overflow

    runtime stack:
    runtime.throw(0x6d84d9)
    c:/go/src/runtime/panic.go:491 +0xad
    runtime.newstack()
    c:/go/src/runtime/stack.c:784 +0x5ef
    runtime.morestack()
    c:/go/src/runtime/asm_amd64.s:324 +0x86



    What am I doing wrong?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Eric Koston at Mar 9, 2015 at 3:21 pm
    I deleted the implemented interface and when I run the application, I've
    got not what I want. &{ObjectIdHex("54f9ba7d5042ea0af0000001")
    map[logged:map[name:Cormier email:cormier@example.com]]}. I expect here by
    map[logged] not a map as result, but athen based type. How can I do that?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Crbrox at Mar 9, 2015 at 5:07 pm
    I don`t know if it would be an acceptable solution for you, but a possible
    way could be
      changing the Session struct to

    type Session struct {

         Id bson.ObjectId `bson:"_id"`

         Logged *Authen `bson:"logged"`

    }


    The library is still doing the most of the work ...

    The Data field could be there still, as a map, just for other more
    "dynamic" stuff

    You could access the Authen information, after retrieved, with s.Logged,
    s.Logged.Name, etc

    A lightly modified version of your code with this approach:
    http://play.golang.org/p/0UPulNTKAj



    El lunes, 9 de marzo de 2015, 16:21:27 (UTC+1), Eric Koston escribió:
    I deleted the implemented interface and when I run the application, I've
    got not what I want. &{ObjectIdHex("54f9ba7d5042ea0af0000001")
    map[logged:map[name:Cormier email:...@example.com <javascript:>]]}. I
    expect here by map[logged] not a map as result, but athen based type. How
    can I do that?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 6, '15 at 3:14p
activeMar 9, '15 at 5:07p
posts4
users2
websitegolang.org

2 users in discussion

Eric Koston: 2 posts Crbrox: 2 posts

People

Translate

site design / logo © 2022 Grokbase