Database related operation files reorganization

This commit is contained in:
Florian Sylvain
2023-01-18 20:55:51 +01:00
parent 0461bb08ef
commit 245ddbc137
5 changed files with 98 additions and 77 deletions
+35
View File
@@ -0,0 +1,35 @@
package database
import (
"context"
"errors"
)
func GetDocuments(location Location, filter interface{}) ([][]byte, error) {
client := GetNewClient()
collection := client.Database(location.Database).Collection(location.Collection)
defer client.Disconnect(context.TODO())
var results [][]byte
cursor, err := collection.Find(context.TODO(), filter)
if err != nil {
return results, errors.New("something is wrong with filter to find the document.")
}
for cursor.TryNext(context.TODO()) {
results = append(results, cursor.Current)
}
return results, nil
}
func GetUniqueDocument(location Location, filter interface{}) ([]byte, error) {
client := GetNewClient()
collection := client.Database(location.Database).Collection(location.Collection)
defer client.Disconnect(context.TODO())
singleResult := collection.FindOne(context.TODO(), filter)
if singleResult.Err() != nil {
return nil, errors.New("no document was found, check the filter or the location.")
}
return singleResult.DecodeBytes()
}