60097653bd4189861.jpg_fo742.jpg

package api_mongo

import (
    // "fmt"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    "log"
    "time"
)

type Person struct {
    Name  string
    Phone string
}
type Collection struct {
    DB   string
    Name string
}

const (
    URL = "mongo.host:27017"
    mongo_user="root"
    mongo_password="123456"
    mongo_database="testdb"

)

func insert(p Person, col Collection) {
    dialInfo := &mgo.DialInfo{
        Addrs:     []string{URL},
        Direct:    false,
        Timeout:   time.Second * 1,
        Database:  mongo_database,
        Source:    "admin",
        Username:  mongo_user,
        Password:  mongo_password,
        PoolLimit: 4096, // Session.SetPoolLimit
    }
    session, err := mgo.DialWithInfo(dialInfo)
    if nil != err {
        panic(err)
    }
    defer session.Close()
    c := session.DB(col.DB).C(col.Name)
    err = c.Insert(p)
    if err != nil {
        log.Fatal(err)
    }
}

// func insert(p Person, col Collection) {
//  session, err := mgo.Dial(URL)
//  if err != nil {
//      panic(err)
//  }
//  defer session.Close()
//  c := session.DB(col.DB).C(col.Name)
//  err = c.Insert(p)
//  if err != nil {
//      log.Fatal(err)
//  }
// }


func findByName(name string, col Collection) Person {
    dialInfo := &mgo.DialInfo{
        Addrs:     []string{URL},
        Direct:    false,
        Timeout:   time.Second * 1,
        Database:  mongo_database,
        Source:    "admin",
        Username:  mongo_user,
        Password:  mongo_password,
        PoolLimit: 4096, // Session.SetPoolLimit
    }
    session, err := mgo.DialWithInfo(dialInfo)
    if nil != err {
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)
    collection := session.DB(col.DB).C(col.Name)
    result := Person{}
    err = collection.Find(bson.M{"name": name}).One(&result)
    if err != nil {
        log.Fatal(err)
    }
    return result
}

func update(p Person, col Collection) {
    dialInfo := &mgo.DialInfo{
        Addrs:     []string{URL},
        Direct:    false,
        Timeout:   time.Second * 1,
        Database:  mongo_database,
        Source:    "admin",
        Username:  mongo_user,
        Password:  mongo_password,
        PoolLimit: 4096, // Session.SetPoolLimit
    }
    session, err := mgo.DialWithInfo(dialInfo)
    if nil != err {
        panic(err)
    }
    defer session.Close()
    collection := session.DB(col.DB).C(col.Name)
    err = collection.Update(bson.M{"name": p.Name}, bson.M{"$set": bson.M{"phone": p.Phone}})
    if err != nil {
        log.Fatal(err)
    }
}

func deleteData(p Person, col Collection) {
    dialInfo := &mgo.DialInfo{
        Addrs:     []string{URL},
        Direct:    false,
        Timeout:   time.Second * 1,
        Database:  mongo_database,
        Source:    "admin",
        Username:  mongo_user,
        Password:  mongo_password,
        PoolLimit: 4096, // Session.SetPoolLimit
    }
    session, err := mgo.DialWithInfo(dialInfo)
    if nil != err {
        panic(err)
    }
    defer session.Close()
    collection := session.DB(col.DB).C(col.Name)
    err = collection.Remove(bson.M{"name": p.Name})
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code