Added getDocuments + refactor

This commit is contained in:
Florian Sylvain
2022-10-14 12:22:36 +02:00
parent 2096dbebc0
commit 688d9797aa
+20 -6
View File
@@ -2,6 +2,7 @@ package internal
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
@@ -22,18 +23,31 @@ func getNewClient() *mongo.Client {
return client
}
func pushDocument(location Location, document interface{}) error {
func getCollection(location Location) *mongo.Collection {
client := getNewClient()
defer client.Disconnect(context.TODO())
return client.Database(location.Database).Collection(location.Collection)
}
coll := client.Database(location.Database).Collection(location.Collection)
_, err := coll.InsertOne(context.TODO(), document)
func pushDocument(location Location, document interface{}) error {
collection := getCollection(location)
_, err := collection.InsertOne(context.TODO(), document)
if err != nil {
return err
}
return nil
}
// func getDocument(database string, collection string) {
// }
func getDocuments(location Location, filter interface{}) ([][]byte, error) {
collection := getCollection(location)
var results [][]byte
cursor, err := collection.Find(context.TODO(), filter)
if err != nil {
return results, errors.New("something is wrong with filter to find document")
}
for cursor.TryNext(context.TODO()) {
results = append(results, cursor.Current)
}
return results, nil
}