update 04.03.24
This commit is contained in:
parent
740323333f
commit
fa863cbd12
5 changed files with 267 additions and 29 deletions
36
util/util.go
Normal file
36
util/util.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InsertRandomData(client *mongo.Client) {
|
||||
persons := []string{"Max", "Moritz", "Hans", "Peter", "Paul", "Klaus", "Karl", "Kai", "Kurt", "Karl-Heinz"}
|
||||
debts := []Debt{}
|
||||
for i := 0; i < 10; i++ {
|
||||
person := Person{Name: persons[i]}
|
||||
collection := client.Database("debtlist").Collection("persons")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
result, err := collection.InsertOne(ctx, person)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for j := 0; j < 3; j++ {
|
||||
debts = append(debts, Debt{PersonID: result.InsertedID.(primitive.ObjectID), Amount: j * 100, Description: "Test", Datetime: time.Now()})
|
||||
}
|
||||
}
|
||||
collection := client.Database("debtlist").Collection("debts")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
for _, debt := range debts {
|
||||
_, err := collection.InsertOne(ctx, debt)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue