main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "fmt"
  6. "gopkg.in/mgo.v2"
  7. "gopkg.in/mgo.v2/bson"
  8. "time"
  9. )
  10. type User struct {
  11. NftpId string `bson:"nftp_id"`
  12. Id string `bson:"id"`
  13. UserName string `bson:"user_name"`
  14. Password string `bson:"password"`
  15. Salt string `bson:"salt"`
  16. Icon string `bson:"icon"`
  17. Type int `bson:"type"`
  18. PhoneNumber string `bson:"phone_number"`
  19. Email string `bson:"email"`
  20. CreateTime int64 `bson:"create_time"`
  21. Description string `bson:"description"`
  22. UpdateTime int64 `bson:"update_time"`
  23. }
  24. func (b User) CollectionName() string {
  25. return "user"
  26. }
  27. func main() {
  28. //连接mongodb
  29. //对于一个集群只需要调用一次 Dial,通过此 Dial 返回的 session 的 New 和 Copy 方法能够创建更多的 session 出来,这些 session 共用底层的连接池
  30. dialInfo := &mgo.DialInfo{
  31. Addrs: []string{"127.0.0.1:27017"},
  32. Database: "test",
  33. Username: "root",
  34. Password: "123456",
  35. Direct: true,
  36. Timeout: time.Second * 10,
  37. PoolLimit: 4096, // Session.SetPoolLimit
  38. }
  39. session, err := mgo.DialWithInfo(dialInfo)
  40. if err != nil {
  41. panic(err)
  42. }
  43. defer session.Close()
  44. //设置一致性模式
  45. //Strong: session 的读写操作总向 primary 服务器发起并使用一个唯一的连接,因此所有的读写操作完全的一致(不存在乱序或者获取到旧数据的问题)
  46. //Monotonic: session 的读操作开始是向某个 secondary 服务器发起(且通过一个唯一的连接),只要出现了一次写操作,session 的连接就会切换至 primary 服务器。
  47. // 由此可见此模式下,能够分散一些读操作到 secondary 服务器,但是读操作不一定能够获得最新的数据。
  48. //Eventual: session 的读操作会向任意的 secondary 服务器发起,多次读操作并不一定使用相同的连接,也就是读操作不一定有序。
  49. // session 的写操作总是向 primary 服务器发起,但是可能使用不同的连接,也就是写操作也不一定有序。
  50. session.SetMode(mgo.Monotonic, true)
  51. //DB()切换database
  52. //C()切换集合collection
  53. c := session.DB("test").C("user")
  54. //Insert()插入
  55. r := sha1.Sum([]byte("ttt" + "4646"))
  56. p := hex.EncodeToString(r[:])
  57. fmt.Println(p)
  58. user := User{Id: "008", UserName: "shuaimei", Password: p, Salt: "ttt", Icon: "", Type: 1, PhoneNumber: "18851177173", Email: "123456@bianjie.ai", CreateTime: time.Now().Unix(), Description: ""}
  59. err = c.Insert(user)
  60. if err != nil {
  61. fmt.Println("insert err: ", err.Error())
  62. }
  63. fmt.Println("insert success")
  64. //Find()查询
  65. //One()查到第一个后返回
  66. //finduser := User{}
  67. //err = c.Find(bson.M{"user_name": "shuai"}).One(&finduser)
  68. //if err != nil {
  69. // fmt.Println("find err: ",err.Error())
  70. //}
  71. //fmt.Println("find success: ",finduser)
  72. //Find()查询
  73. //All()返回所有符合条件的
  74. var findusers []User
  75. err = c.Find(bson.M{"user_name": "shuai"}).All(&findusers)
  76. if err != nil {
  77. fmt.Println("find all err: ", err.Error())
  78. }
  79. fmt.Println("find all success: ", findusers)
  80. //Update()更新
  81. //err = c.Update(bson.M{"password":"123"},bson.M{"$set":bson.M{"password":"857"}})
  82. //if err != nil {
  83. // fmt.Println("update err: ",err.Error())
  84. //}
  85. //fmt.Println("update success: ")
  86. //Update()更新
  87. //err = c.Update(bson.M{"password":"123"},bson.M{"$set":bson.M{"password":"857"}})
  88. //if err != nil {
  89. // fmt.Println("update err: ",err.Error())
  90. //}
  91. //fmt.Println("update success: ",findusers)
  92. //user2 := User{UserName:"miuye",Password:"7173",Salt:"adgadf"}
  93. //err = c.Update(bson.M{"id":"001"},bson.M{"$set":user2})
  94. //if err != nil {
  95. // fmt.Println("update err: ",err.Error())
  96. //}
  97. //fmt.Println("update success: ",findusers)
  98. }