-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
50 lines (42 loc) · 1022 Bytes
/
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
package mongolib
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Collection struct {
*mongo.Collection
}
func (coll *Collection) FindByID(ctx context.Context, id primitive.ObjectID) Result {
filter := bson.D{{"_id", id}}
res := coll.FindOne(ctx, filter)
return &SingleResult{
SingleResult: res,
}
}
func (coll *Collection) Save(ctx context.Context, id primitive.ObjectID, data interface{}) error {
update := bson.D{{"$set", data}}
filter := bson.D{{"_id", id}}
opt := options.Update().SetUpsert(true)
if _, err := coll.UpdateOne(ctx, filter, update, opt); err != nil {
return err
}
return nil
}
func (coll *Collection) Query() Query {
return Query{
coll: coll,
filter: bson.A{},
limit: 0,
offset: 0,
sort: bson.D{},
}
}
func (coll *Collection) Aggregate() Aggregate {
return Aggregate{
coll: coll,
pipeline: mongo.Pipeline{},
}
}