-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongo.go
136 lines (98 loc) · 3.4 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Client struct {
*mongo.Database
logger Logger
metrics Metrics
}
func New(conf Config, logger Logger, metrics Metrics) *Client {
logger.Logf("using gofr-mongo as external DB for mongo")
m, err := mongo.Connect(context.Background(), options.Client().ApplyURI(conf.Get("MONGO_URI")))
if err != nil {
logger.Errorf("error connecting to mongoDB, err:%v", err)
return nil
}
return &Client{
Database: m.Database(conf.Get("MONGO_DATABASE")),
logger: logger,
metrics: metrics,
}
}
func (c *Client) InsertOne(ctx context.Context, collection string, document interface{}) (interface{}, error) {
c.logger.Debug("InsertOne")
return c.Database.Collection(collection).InsertOne(ctx, document)
}
func (c *Client) Find(ctx context.Context, collection string, filter interface{}, results interface{}) error {
c.logger.Debug("Find")
cur, err := c.Database.Collection(collection).Find(ctx, filter)
if err != nil {
return err
}
defer cur.Close(ctx)
if err := cur.All(ctx, results); err != nil {
return err
}
return nil
}
func (c *Client) FindOne(ctx context.Context, collection string, filter interface{}, result interface{}) error {
c.logger.Debug("FindOne")
res := c.Database.Collection(collection).FindOne(ctx, filter)
if res.Err() != nil {
return res.Err()
}
b, err := res.Raw()
if err != nil {
return err
}
return bson.Unmarshal(b, result)
}
func (c *Client) InsertMany(ctx context.Context, collection string, documents []interface{}) ([]interface{}, error) {
c.logger.Debug("InsertMany")
res, err := c.Database.Collection(collection).InsertMany(ctx, documents)
if err != nil {
return nil, err
}
return res.InsertedIDs, nil
}
func (c *Client) DeleteOne(ctx context.Context, collection string, filter interface{}) (int64, error) {
c.logger.Debug("DeleteOne")
res, err := c.Database.Collection(collection).DeleteOne(ctx, filter)
if err != nil {
return 0, err
}
return res.DeletedCount, nil
}
func (c *Client) DeleteMany(ctx context.Context, collection string, filter interface{}) (int64, error) {
res, err := c.Database.Collection(collection).DeleteMany(ctx, filter)
if err != nil {
return 0, err
}
return res.DeletedCount, nil
}
func (c *Client) UpdateByID(ctx context.Context, collection string, id interface{}, update interface{}) (int64, error) {
c.logger.Debug("UpdateByID")
res, err := c.Database.Collection(collection).UpdateByID(ctx, id, update)
return res.ModifiedCount, err
}
func (c *Client) UpdateOne(ctx context.Context, collection string, filter interface{}, update interface{}) error {
c.logger.Debug("UpdateOne")
_, err := c.Database.Collection(collection).UpdateOne(ctx, filter, update)
return err
}
func (c *Client) UpdateMany(ctx context.Context, collection string, filter interface{}, update interface{}) (int64, error) {
c.logger.Debug("updateMany")
res, err := c.Database.Collection(collection).UpdateMany(ctx, filter, update)
return res.ModifiedCount, err
}
func (c *Client) CountDocuments(ctx context.Context, collection string, filter interface{}) (int64, error) {
c.logger.Debug("CountDocuments")
return c.Database.Collection(collection).CountDocuments(ctx, filter)
}
func (c *Client) Drop(ctx context.Context, collection string) error {
return c.Database.Collection(collection).Drop(ctx)
}