forked from sv-tools/mongoifc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
300 lines (264 loc) · 7.92 KB
/
collection.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package mongoifc
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Collection is an interface for `mongo.Collection` structure
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Collection
type Collection interface {
Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (Cursor, error)
BulkWrite(
ctx context.Context,
models []mongo.WriteModel,
opts ...*options.BulkWriteOptions,
) (*mongo.BulkWriteResult, error)
Clone(opts ...*options.CollectionOptions) (Collection, error)
CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
Database() Database
DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
Distinct(
ctx context.Context,
fieldName string,
filter interface{},
opts ...*options.DistinctOptions,
) ([]interface{}, error)
Drop(ctx context.Context) error
EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (Cursor, error)
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) SingleResult
FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) SingleResult
FindOneAndReplace(
ctx context.Context,
filter interface{},
replacement interface{},
opts ...*options.FindOneAndReplaceOptions,
) SingleResult
FindOneAndUpdate(
ctx context.Context,
filter interface{},
update interface{},
opts ...*options.FindOneAndUpdateOptions,
) SingleResult
Indexes() IndexView
InsertMany(
ctx context.Context,
documents []interface{},
opts ...*options.InsertManyOptions,
) (*mongo.InsertManyResult, error)
InsertOne(
ctx context.Context,
document interface{},
opts ...*options.InsertOneOptions,
) (*mongo.InsertOneResult, error)
Name() string
ReplaceOne(
ctx context.Context,
filter interface{},
replacement interface{},
opts ...*options.ReplaceOptions,
) (*mongo.UpdateResult, error)
SearchIndexes() SearchIndexView
UpdateByID(
ctx context.Context,
id interface{},
update interface{},
opts ...*options.UpdateOptions,
) (*mongo.UpdateResult, error)
UpdateMany(
ctx context.Context,
filter interface{},
update interface{},
opts ...*options.UpdateOptions,
) (*mongo.UpdateResult, error)
UpdateOne(
ctx context.Context,
filter interface{},
update interface{},
opts ...*options.UpdateOptions,
) (*mongo.UpdateResult, error)
Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (ChangeStream, error)
}
type collection struct {
co *mongo.Collection
db *database
}
func (c *collection) Aggregate(
ctx context.Context,
pipeline interface{},
opts ...*options.AggregateOptions,
) (Cursor, error) {
cr, err := c.co.Aggregate(ctx, pipeline, opts...)
if err != nil {
return nil, err
}
return wrapCursor(cr), nil
}
func (c *collection) BulkWrite(
ctx context.Context,
models []mongo.WriteModel,
opts ...*options.BulkWriteOptions,
) (*mongo.BulkWriteResult, error) {
return c.co.BulkWrite(ctx, models, opts...)
}
func (c *collection) Clone(opts ...*options.CollectionOptions) (Collection, error) {
co, err := c.co.Clone(opts...)
if err != nil {
return nil, err
}
return wrapCollection(co, c.db), nil
}
func (c *collection) CountDocuments(
ctx context.Context,
filter interface{},
opts ...*options.CountOptions,
) (int64, error) {
return c.co.CountDocuments(ctx, filter, opts...)
}
func (c *collection) Database() Database {
return c.db
}
func (c *collection) DeleteMany(
ctx context.Context,
filter interface{},
opts ...*options.DeleteOptions,
) (*mongo.DeleteResult, error) {
return c.co.DeleteMany(ctx, filter, opts...)
}
func (c *collection) DeleteOne(
ctx context.Context,
filter interface{},
opts ...*options.DeleteOptions,
) (*mongo.DeleteResult, error) {
return c.co.DeleteOne(ctx, filter, opts...)
}
func (c *collection) Distinct(
ctx context.Context,
fieldName string,
filter interface{},
opts ...*options.DistinctOptions,
) ([]interface{}, error) {
return c.co.Distinct(ctx, fieldName, filter, opts...)
}
func (c *collection) Drop(ctx context.Context) error {
return c.co.Drop(ctx)
}
func (c *collection) EstimatedDocumentCount(
ctx context.Context,
opts ...*options.EstimatedDocumentCountOptions,
) (int64, error) {
return c.co.EstimatedDocumentCount(ctx, opts...)
}
func (c *collection) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (Cursor, error) {
cr, err := c.co.Find(ctx, filter, opts...)
if err != nil {
return nil, err
}
return wrapCursor(cr), nil
}
func (c *collection) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) SingleResult {
return wrapSingleResult(c.co.FindOne(ctx, filter, opts...))
}
func (c *collection) FindOneAndDelete(
ctx context.Context,
filter interface{},
opts ...*options.FindOneAndDeleteOptions,
) SingleResult {
return wrapSingleResult(c.co.FindOneAndDelete(ctx, filter, opts...))
}
func (c *collection) FindOneAndReplace(
ctx context.Context,
filter interface{},
replacement interface{},
opts ...*options.FindOneAndReplaceOptions,
) SingleResult {
return wrapSingleResult(c.co.FindOneAndReplace(ctx, filter, replacement, opts...))
}
func (c *collection) FindOneAndUpdate(
ctx context.Context,
filter interface{},
update interface{},
opts ...*options.FindOneAndUpdateOptions,
) SingleResult {
return wrapSingleResult(c.co.FindOneAndUpdate(ctx, filter, update, opts...))
}
func (c *collection) Indexes() IndexView {
iv := c.co.Indexes()
return wrapIndexView(&iv)
}
func (c *collection) InsertMany(
ctx context.Context,
documents []interface{},
opts ...*options.InsertManyOptions,
) (*mongo.InsertManyResult, error) {
return c.co.InsertMany(ctx, documents, opts...)
}
func (c *collection) InsertOne(
ctx context.Context,
document interface{},
opts ...*options.InsertOneOptions,
) (*mongo.InsertOneResult, error) {
return c.co.InsertOne(ctx, document, opts...)
}
func (c *collection) Name() string {
return c.co.Name()
}
func (c *collection) ReplaceOne(
ctx context.Context,
filter interface{},
replacement interface{},
opts ...*options.ReplaceOptions,
) (*mongo.UpdateResult, error) {
return c.co.ReplaceOne(ctx, filter, replacement, opts...)
}
func (c *collection) SearchIndexes() SearchIndexView {
siv := c.co.SearchIndexes()
return wrapSearchIndexView(&siv)
}
func (c *collection) UpdateByID(
ctx context.Context,
id interface{},
update interface{},
opts ...*options.UpdateOptions,
) (*mongo.UpdateResult, error) {
return c.co.UpdateByID(ctx, id, update, opts...)
}
func (c *collection) UpdateMany(
ctx context.Context,
filter interface{},
update interface{},
opts ...*options.UpdateOptions,
) (*mongo.UpdateResult, error) {
return c.co.UpdateMany(ctx, filter, update, opts...)
}
func (c *collection) UpdateOne(
ctx context.Context,
filter interface{},
update interface{},
opts ...*options.UpdateOptions,
) (*mongo.UpdateResult, error) {
return c.co.UpdateOne(ctx, filter, update, opts...)
}
func (c *collection) Watch(
ctx context.Context,
pipeline interface{},
opts ...*options.ChangeStreamOptions,
) (ChangeStream, error) {
cs, err := c.co.Watch(ctx, pipeline, opts...)
if err != nil {
return nil, err
}
return wrapChangeStream(cs), nil
}
func wrapCollection(co *mongo.Collection, db *database) Collection {
return &collection{co: co, db: db}
}
// WrapCollection returns an instance of Collection interface for given mongo.Collection object
func WrapCollection(co *mongo.Collection) Collection {
return wrapCollection(co, WrapDatabase(co.Database()).(*database))
}
// UnWrapCollection returns original mongo.Collection
func UnWrapCollection(co Collection) *mongo.Collection {
return co.(*collection).co
}