-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
102 lines (82 loc) · 2.79 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
package mgm
import (
"github.com/Kamva/mgm/builder"
"github.com/Kamva/mgm/field"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Collection performs operations on models and given Mongodb collection
type Collection struct {
*mongo.Collection
}
// FindByID method find a doc and decode it to model, otherwise return error.
// id field can be any value that if passed to `PrepareID` method, it return
// valid id(e.g string,bson.ObjectId).
func (coll *Collection) FindByID(id interface{}, model Model) error {
id, err := model.PrepareID(id)
if err != nil {
return err
}
return first(coll, bson.M{field.ID: id}, model)
}
// First method search and return first document of search result.
func (coll *Collection) First(filter interface{}, model Model, opts ...*options.FindOneOptions) error {
return first(coll, filter, model, opts...)
}
// Create method insert new model into database.
func (coll *Collection) Create(model Model) error {
return create(coll, model)
}
// Update function update save changed model into database.
// On call to this method also mgm call to model's updating,updated,
// saving,saved hooks.
func (coll *Collection) Update(model Model) error {
return update(coll, model)
}
// Save method save model(insert,update).
func (coll *Collection) Save(model Model) error {
if model.IsNew() {
return create(coll, model)
}
return update(coll, model)
}
// Delete method delete model (doc) from collection.
// If you want to doing something on deleting some model
// use hooks, don't need to override this method.
func (coll *Collection) Delete(model Model) error {
return del(coll, model)
}
// SimpleFind find and decode result to results.
func (coll *Collection) SimpleFind(results interface{}, filter interface{}, opts ...*options.FindOptions) error {
ctx := ctx()
cur, err := coll.Find(ctx, filter, opts...)
if err != nil {
return err
}
return cur.All(ctx, results)
}
//--------------------------------
// Aggregation methods
//--------------------------------
// SimpleAggregate doing simple aggregation and decode aggregate result to the results.
// stages value can be Operator|bson.M
func (coll *Collection) SimpleAggregate(results interface{}, stages ...interface{}) error {
cur, err := coll.SimpleAggregateCursor(stages...)
if err != nil {
return err
}
return cur.All(ctx(), results)
}
// SimpleAggregateCursor doing simple aggregation and return cursor.
func (coll *Collection) SimpleAggregateCursor(stages ...interface{}) (*mongo.Cursor, error) {
pipeline := bson.A{}
for _, stage := range stages {
if operator, ok := stage.(builder.Operator); ok {
pipeline = append(pipeline, builder.S(operator))
} else {
pipeline = append(pipeline, stage)
}
}
return coll.Aggregate(ctx(), pipeline, nil)
}