| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package main
- import (
- "crypto/sha1"
- "encoding/hex"
- "fmt"
- "gopkg.in/mgo.v2"
- "gopkg.in/mgo.v2/bson"
- "time"
- )
- type User struct {
- NftpId string `bson:"nftp_id"`
- Id string `bson:"id"`
- UserName string `bson:"user_name"`
- Password string `bson:"password"`
- Salt string `bson:"salt"`
- Icon string `bson:"icon"`
- Type int `bson:"type"`
- PhoneNumber string `bson:"phone_number"`
- Email string `bson:"email"`
- CreateTime int64 `bson:"create_time"`
- Description string `bson:"description"`
- UpdateTime int64 `bson:"update_time"`
- }
- func (b User) CollectionName() string {
- return "user"
- }
- func main() {
- //连接mongodb
- //对于一个集群只需要调用一次 Dial,通过此 Dial 返回的 session 的 New 和 Copy 方法能够创建更多的 session 出来,这些 session 共用底层的连接池
- dialInfo := &mgo.DialInfo{
- Addrs: []string{"127.0.0.1:27017"},
- Database: "test",
- Username: "root",
- Password: "123456",
- Direct: true,
- Timeout: time.Second * 10,
- PoolLimit: 4096, // Session.SetPoolLimit
- }
- session, err := mgo.DialWithInfo(dialInfo)
- if err != nil {
- panic(err)
- }
- defer session.Close()
- //设置一致性模式
- //Strong: session 的读写操作总向 primary 服务器发起并使用一个唯一的连接,因此所有的读写操作完全的一致(不存在乱序或者获取到旧数据的问题)
- //Monotonic: session 的读操作开始是向某个 secondary 服务器发起(且通过一个唯一的连接),只要出现了一次写操作,session 的连接就会切换至 primary 服务器。
- // 由此可见此模式下,能够分散一些读操作到 secondary 服务器,但是读操作不一定能够获得最新的数据。
- //Eventual: session 的读操作会向任意的 secondary 服务器发起,多次读操作并不一定使用相同的连接,也就是读操作不一定有序。
- // session 的写操作总是向 primary 服务器发起,但是可能使用不同的连接,也就是写操作也不一定有序。
- session.SetMode(mgo.Monotonic, true)
- //DB()切换database
- //C()切换集合collection
- c := session.DB("test").C("user")
- //Insert()插入
- r := sha1.Sum([]byte("ttt" + "4646"))
- p := hex.EncodeToString(r[:])
- fmt.Println(p)
- user := User{Id: "008", UserName: "shuaimei", Password: p, Salt: "ttt", Icon: "", Type: 1, PhoneNumber: "18851177173", Email: "123456@bianjie.ai", CreateTime: time.Now().Unix(), Description: ""}
- err = c.Insert(user)
- if err != nil {
- fmt.Println("insert err: ", err.Error())
- }
- fmt.Println("insert success")
- //Find()查询
- //One()查到第一个后返回
- //finduser := User{}
- //err = c.Find(bson.M{"user_name": "shuai"}).One(&finduser)
- //if err != nil {
- // fmt.Println("find err: ",err.Error())
- //}
- //fmt.Println("find success: ",finduser)
- //Find()查询
- //All()返回所有符合条件的
- var findusers []User
- err = c.Find(bson.M{"user_name": "shuai"}).All(&findusers)
- if err != nil {
- fmt.Println("find all err: ", err.Error())
- }
- fmt.Println("find all success: ", findusers)
- //Update()更新
- //err = c.Update(bson.M{"password":"123"},bson.M{"$set":bson.M{"password":"857"}})
- //if err != nil {
- // fmt.Println("update err: ",err.Error())
- //}
- //fmt.Println("update success: ")
- //Update()更新
- //err = c.Update(bson.M{"password":"123"},bson.M{"$set":bson.M{"password":"857"}})
- //if err != nil {
- // fmt.Println("update err: ",err.Error())
- //}
- //fmt.Println("update success: ",findusers)
- //user2 := User{UserName:"miuye",Password:"7173",Salt:"adgadf"}
- //err = c.Update(bson.M{"id":"001"},bson.M{"$set":user2})
- //if err != nil {
- // fmt.Println("update err: ",err.Error())
- //}
- //fmt.Println("update success: ",findusers)
- }
|