码迷,mamicode.com
首页 > 数据库 > 详细

golang学习之mgo操作mongodb

时间:2016-07-08 23:07:33      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

mgo是mongodb的golang驱动,测试代码:

// mgotest project main.go
package main

import (
    "fmt"
    "time"

    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type User struct {
    Id        bson.ObjectId `bson:"_id"`
    Username  string        `bson:"name"`
    Pass      string        `bson:"pass"`
    Regtime   int64         `bson:"regtime"`
    Interests []string      `bson:"interests"`
}

const URL String = "127.0.0.1:27017"

var c *mgo.Collection
var session *mgo.Session

func (user User) ToString() string {
    return fmt.Sprintf("%#v", user)
}

func init() {
    session, _ = mgo.Dial(URL)
    //切换到数据库
    db := session.DB("blog")
    //切换到collection
    c = db.C("mgotest")
}

//新增数据
func add() {
    //    defer session.Close()
    stu1 := new(User)
    stu1.Id = bson.NewObjectId()
    stu1.Username = "stu1_name"
    stu1.Pass = "stu1_pass"
    stu1.Regtime = time.Now().Unix()
    stu1.Interests = []string{"象棋", "游泳", "跑步"}
    err := c.Insert(stu1)
    if err == nil {
        fmt.Println("插入成功")
    } else {
        fmt.Println(err.Error())
        defer panic(err)
    }
}

//查询
func find() {
    //    defer session.Close()
    var users []User
    //    c.Find(nil).All(&users)
    c.Find(bson.M{"name": "stu1_name"}).All(&users)
    for _, value := range users {
        fmt.Println(value.ToString())
    }
    //根据ObjectId进行查询
    idStr := "577fb2d1cde67307e819133d"
    objectId := bson.ObjectIdHex(idStr)
    user := new(User)
    c.Find(bson.M{"_id": objectId}).One(user)
    fmt.Println(user)
}

//根据id进行修改
func update() {
    interests := []string{"象棋", "游泳", "跑步"}
    err := c.Update(bson.M{"_id": bson.ObjectIdHex("577fb2d1cde67307e819133d")}, bson.M{"$set": bson.M{
        "name":      "修改后的name",
        "pass":      "修改后的pass",
        "regtime":   time.Now().Unix(),
        "interests": interests,
    }})
    if err != nil {
        fmt.Println("修改失败")
    } else {
        fmt.Println("修改成功")
    }
}

//删除
func del() {
    err := c.Remove(bson.M{"_id": bson.ObjectIdHex("577fb2d1cde67307e819133d")})
    if err != nil {
        fmt.Println("删除失败" + err.Error())
    } else {
        fmt.Println("删除成功")
    }
}
func main() {
    add()
    find()
    update()
    del()
}

 

golang学习之mgo操作mongodb

标签:

原文地址:http://www.cnblogs.com/caiya928/p/5654781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!