main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/qiniu/qmgo"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "go.mongodb.org/mongo-driver/mongo/readpref"
  8. )
  9. type User struct {
  10. NftpId string `bson:"nftp_id"`
  11. Id string `bson:"id"`
  12. UserName string `bson:"user_name"`
  13. Password string `bson:"password"`
  14. Salt string `bson:"salt"`
  15. Icon string `bson:"icon"`
  16. Type int `bson:"type"`
  17. PhoneNumber string `bson:"phone_number"`
  18. Email string `bson:"email"`
  19. CreateTime int64 `bson:"create_time"`
  20. Description string `bson:"description"`
  21. UpdateTime int64 `bson:"update_time"`
  22. }
  23. func main() {
  24. var maxPoolSize uint64 = 4096
  25. ctx := context.Background()
  26. url := "mongodb://192.168.1.7:27017/?connect=direct&authSource=test"
  27. client, _ := qmgo.NewClient(ctx, &qmgo.Config{
  28. Uri: url,
  29. ReadPreference: &qmgo.ReadPref{
  30. MaxStalenessMS: 90000,
  31. Mode: readpref.SecondaryPreferredMode,
  32. },
  33. MaxPoolSize: &maxPoolSize,
  34. })
  35. coll := client.Database("test").Collection("user")
  36. var user User
  37. err := coll.Find(ctx, bson.M{"id": "004"}).One(&user)
  38. if err != nil {
  39. fmt.Println(err.Error())
  40. }
  41. fmt.Println(user)
  42. }