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 ( import (
"context" "context"
"errors"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
@@ -22,18 +23,31 @@ func getNewClient() *mongo.Client {
return client return client
} }
func pushDocument(location Location, document interface{}) error { func getCollection(location Location) *mongo.Collection {
client := getNewClient() client := getNewClient()
defer client.Disconnect(context.TODO()) defer client.Disconnect(context.TODO())
return client.Database(location.Database).Collection(location.Collection)
}
coll := client.Database(location.Database).Collection(location.Collection) func pushDocument(location Location, document interface{}) error {
_, err := coll.InsertOne(context.TODO(), document) collection := getCollection(location)
_, err := collection.InsertOne(context.TODO(), document)
if err != nil { if err != nil {
return err return err
} }
return nil 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
}