mongodb_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package test
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "go.mongodb.org/mongo-driver/mongo"
  8. "go.mongodb.org/mongo-driver/mongo/options"
  9. "log"
  10. "testing"
  11. "time"
  12. )
  13. var (
  14. Mongodb_Url = "mongodb://192.168.11.112:27017"
  15. Mongodb_DB = "yunlot"
  16. Mongodb_Username = "yunlot8"
  17. Mongodb_Password = "yunlot123"
  18. )
  19. func Connect() (*mongo.Database, error) {
  20. credential := options.Credential{
  21. Username: Mongodb_Username,
  22. Password: Mongodb_Password,
  23. }
  24. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  25. defer cancel()
  26. client, err := mongo.Connect(ctx, options.Client().ApplyURI(Mongodb_Url).SetAuth(credential).SetMaxPoolSize(10))
  27. if err != nil {
  28. log.Print(err)
  29. }
  30. return client.Database(Mongodb_DB), err
  31. }
  32. func TestNamex(t *testing.T) {
  33. // 连接到MongoDB
  34. DB, err := Connect()
  35. if err != nil {
  36. panic(any(err))
  37. }
  38. collection := DB.Collection("aaa")
  39. _, err = collection.InsertOne(context.TODO(), bson.D{{"text", "text"}})
  40. if err != nil {
  41. t.Fatalf("error inserting: %v", err)
  42. }
  43. students := []interface{}{bson.D{{"text", "2text"}}, bson.D{{"text", "1text"}}}
  44. insertManyResult, err := collection.InsertMany(context.TODO(), students)
  45. if err != nil {
  46. panic(any(err))
  47. }
  48. fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs)
  49. }
  50. // 根据 条件、排序、分页 获取列表
  51. func TestMongodb_list(t *testing.T) {
  52. // 连接到MongoDB
  53. DB, err := Connect()
  54. if err != nil {
  55. panic(any(err))
  56. }
  57. collection := DB.Collection("2023445039284316_params_varData")
  58. // 条件
  59. jsonFind := `{"$or":[{"name":"TempSet"},{"value":"26"}]}`
  60. // 排序
  61. jsonSort := `{"value": 1}`
  62. // 分页
  63. page := 2 // 当前页码
  64. pageSize := 2 // 每页文档数量
  65. // 解码JSON
  66. var jsonFindMap map[string]interface{}
  67. err = json.Unmarshal([]byte(jsonFind), &jsonFindMap)
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. // 解码JSON
  72. var jsonSortMap map[string]interface{}
  73. err = json.Unmarshal([]byte(jsonSort), &jsonSortMap)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. options := options.Find().SetSkip(int64((page - 1) * pageSize)).SetLimit(int64(pageSize))
  78. options = options.SetSort(bson.M(jsonSortMap))
  79. cursor, err := collection.Find(context.Background(), bson.M(jsonFindMap), options)
  80. if err != nil {
  81. panic(any(err))
  82. }
  83. defer cursor.Close(context.Background())
  84. var results []bson.M
  85. if err = cursor.All(context.Background(), &results); err != nil {
  86. panic(any(err))
  87. }
  88. jsonBytes, err := json.MarshalIndent(results, "", " ")
  89. if err != nil {
  90. panic(any(err))
  91. }
  92. fmt.Println(string(jsonBytes))
  93. // 执行计数操作
  94. count, err := collection.CountDocuments(context.Background(), bson.M(jsonFindMap))
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. fmt.Println("Count:", count)
  99. }
  100. func Data_Add(JointTab string, bson_r *bson.M) {
  101. // 连接到MongoDB
  102. DB, err := Connect()
  103. if err != nil {
  104. panic(any(err))
  105. }
  106. //fmt.Println("JointTab:", JointTab)
  107. collection := DB.Collection(JointTab)
  108. _, err = collection.InsertOne(context.TODO(), bson_r)
  109. if err != nil {
  110. panic(any(err))
  111. }
  112. //
  113. //students := []interface{}{bson.D{{"text", "2text"}}, bson.D{{"text", "1text"}}}
  114. //insertManyResult, err := collection.InsertMany(context.TODO(), students)
  115. //if err != nil {
  116. // panic(any(err))
  117. //}
  118. //fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs)
  119. }