您的位置:首页 > 数据库 > Mongodb

go sample - mongodb

2015-09-10 15:38 507 查看
简单的mongodb 操作

package mainimport (    "fmt"    "gopkg.in/mgo.v2"    "gopkg.in/mgo.v2/bson")type Person struct {    Name  string    Phone string}func main() {    session, err := mgo.Dial("10.0.2.49:29000")    //session, err := mgo.Dial("10.0.2.49:29000")    if err != nil {        panic(err)    }    defer session.Close()    // Optional. Switch the session to a monotonic behavior.    session.SetMode(mgo.Monotonic, true)    c := session.DB("test").C("people")    err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},        &Person{"Cla", "+55 53 8402 8510"})    if err != nil {        panic(err)    }    result := Person{}    err = c.Find(bson.M{"name": "Ale"}).One(&result)    if err != nil {        panic(err)    }    a := result.Phone    fmt.Println("Phone:", a)}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: