36 lines
1 KiB
Go
36 lines
1 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|