/
find_many_mongo.go
47 lines (37 loc) · 1.34 KB
/
find_many_mongo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package mongodb
import (
"context"
logger "github.com/paulmuenzner/powerplantmanager/utils/logs"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func (client *Client) FindManyInMongo(databaseName string, filter bson.M, collection string, sort bson.D, result interface{}) error {
// Create a session for the database
session, err := client.MongoDB.StartSession()
if err != nil {
return err
}
defer session.EndSession(context.Background())
// Select the database and collection
db := client.MongoDB.Database(databaseName)
col := db.Collection(collection)
// Set options for Find
options := options.Find().SetSort(sort)
// Insert the data into the collection
cur, err := col.Find(context.Background(), filter, options)
if err != nil {
if err == mongo.ErrNoDocuments {
return err
}
logger.GetLogger().Error("Error when querying collection "+collection+" of database "+databaseName+" in 'FindManyInMongo'. Error: ", err)
return err
}
defer cur.Close(context.Background())
// Decode the results into the provided result interface
if err := cur.All(context.Background(), result); err != nil {
logger.GetLogger().Error("Error when decoding documents "+collection+" of database "+databaseName+" in 'FindManyInMongo'. Error: ", err)
return err
}
return nil
}