package test import ( "context" "encoding/json" "fmt" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "log" "testing" "time" ) var ( Mongodb_Url = "mongodb://192.168.11.112:27017" Mongodb_DB = "yunlot" Mongodb_Username = "yunlot8" Mongodb_Password = "yunlot123" ) func Connect() (*mongo.Database, error) { credential := options.Credential{ Username: Mongodb_Username, Password: Mongodb_Password, } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() client, err := mongo.Connect(ctx, options.Client().ApplyURI(Mongodb_Url).SetAuth(credential).SetMaxPoolSize(10)) if err != nil { log.Print(err) } return client.Database(Mongodb_DB), err } func TestNamex(t *testing.T) { // 连接到MongoDB DB, err := Connect() if err != nil { panic(any(err)) } collection := DB.Collection("aaa") _, err = collection.InsertOne(context.TODO(), bson.D{{"text", "text"}}) if err != nil { t.Fatalf("error inserting: %v", err) } students := []interface{}{bson.D{{"text", "2text"}}, bson.D{{"text", "1text"}}} insertManyResult, err := collection.InsertMany(context.TODO(), students) if err != nil { panic(any(err)) } fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs) } // 根据 条件、排序、分页 获取列表 func TestMongodb_list(t *testing.T) { // 连接到MongoDB DB, err := Connect() if err != nil { panic(any(err)) } collection := DB.Collection("2023445039284316_params_varData") // 条件 jsonFind := `{"$or":[{"name":"TempSet"},{"value":"26"}]}` // 排序 jsonSort := `{"value": 1}` // 分页 page := 2 // 当前页码 pageSize := 2 // 每页文档数量 // 解码JSON var jsonFindMap map[string]interface{} err = json.Unmarshal([]byte(jsonFind), &jsonFindMap) if err != nil { log.Fatal(err) } // 解码JSON var jsonSortMap map[string]interface{} err = json.Unmarshal([]byte(jsonSort), &jsonSortMap) if err != nil { log.Fatal(err) } options := options.Find().SetSkip(int64((page - 1) * pageSize)).SetLimit(int64(pageSize)) options = options.SetSort(bson.M(jsonSortMap)) cursor, err := collection.Find(context.Background(), bson.M(jsonFindMap), options) if err != nil { panic(any(err)) } defer cursor.Close(context.Background()) var results []bson.M if err = cursor.All(context.Background(), &results); err != nil { panic(any(err)) } jsonBytes, err := json.MarshalIndent(results, "", " ") if err != nil { panic(any(err)) } fmt.Println(string(jsonBytes)) // 执行计数操作 count, err := collection.CountDocuments(context.Background(), bson.M(jsonFindMap)) if err != nil { log.Fatal(err) } fmt.Println("Count:", count) } func Data_Add(JointTab string, bson_r *bson.M) { // 连接到MongoDB DB, err := Connect() if err != nil { panic(any(err)) } //fmt.Println("JointTab:", JointTab) collection := DB.Collection(JointTab) _, err = collection.InsertOne(context.TODO(), bson_r) if err != nil { panic(any(err)) } // //students := []interface{}{bson.D{{"text", "2text"}}, bson.D{{"text", "1text"}}} //insertManyResult, err := collection.InsertMany(context.TODO(), students) //if err != nil { // panic(any(err)) //} //fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs) }