-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
collection.go
156 lines (130 loc) · 3.96 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
package mango
import (
"context"
"github.com/amorist/mango/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Collection mongo-driver collection
type Collection struct {
collection *mongo.Collection
}
// Find finds docs by given filter
func (c *Collection) Find(filter interface{}) *Session {
return &Session{filter: filter, collection: c.collection}
}
// Insert inserts a single document into the collection.
func (c *Collection) Insert(document interface{}) error {
var err error
if _, err = c.collection.InsertOne(context.TODO(), document); err != nil {
return err
}
return nil
}
// InsertWithResult inserts a single document into the collection and returns insert one result.
func (c *Collection) InsertWithResult(document interface{}) (result *mongo.InsertOneResult, err error) {
result, err = c.collection.InsertOne(context.TODO(), document)
return
}
// InsertAll inserts the provided documents.
func (c *Collection) InsertAll(documents []interface{}) error {
var err error
if _, err = c.collection.InsertMany(context.TODO(), documents); err != nil {
return err
}
return nil
}
// InsertAllWithResult inserts the provided documents and returns insert many result.
func (c *Collection) InsertAllWithResult(documents []interface{}) (result *mongo.InsertManyResult, err error) {
result, err = c.collection.InsertMany(context.TODO(), documents)
return
}
// Update updates a single document in the collection.
func (c *Collection) Update(selector interface{}, update interface{}, upsert ...bool) error {
if selector == nil {
selector = bson.D{}
}
var err error
opt := options.Update()
for _, arg := range upsert {
if arg {
opt.SetUpsert(arg)
}
}
if _, err = c.collection.UpdateOne(context.TODO(), selector, update, opt); err != nil {
return err
}
return nil
}
// UpdateWithResult updates a single document in the collection and returns update result.
func (c *Collection) UpdateWithResult(selector interface{}, update interface{}, upsert ...bool) (result *mongo.UpdateResult, err error) {
if selector == nil {
selector = bson.D{}
}
opt := options.Update()
for _, arg := range upsert {
if arg {
opt.SetUpsert(arg)
}
}
result, err = c.collection.UpdateOne(context.TODO(), selector, update, opt)
return
}
// UpdateID updates a single document in the collection by id
func (c *Collection) UpdateID(id interface{}, update interface{}) error {
return c.Update(bson.M{"_id": id}, update)
}
// UpdateAll updates multiple documents in the collection.
func (c *Collection) UpdateAll(selector interface{}, update interface{}, upsert ...bool) (*mongo.UpdateResult, error) {
if selector == nil {
selector = bson.D{}
}
var err error
opt := options.Update()
for _, arg := range upsert {
if arg {
opt.SetUpsert(arg)
}
}
var updateResult *mongo.UpdateResult
if updateResult, err = c.collection.UpdateMany(context.TODO(), selector, update, opt); err != nil {
return updateResult, err
}
return updateResult, nil
}
// Remove deletes a single document from the collection.
func (c *Collection) Remove(selector interface{}) error {
if selector == nil {
selector = bson.D{}
}
var err error
if _, err = c.collection.DeleteOne(context.TODO(), selector); err != nil {
return err
}
return nil
}
// RemoveID deletes a single document from the collection by id.
func (c *Collection) RemoveID(id interface{}) error {
return c.Remove(id)
}
// RemoveAll deletes multiple documents from the collection.
func (c *Collection) RemoveAll(selector interface{}) error {
if selector == nil {
selector = bson.D{}
}
var err error
if _, err = c.collection.DeleteMany(context.TODO(), selector); err != nil {
return err
}
return nil
}
// Count gets the number of documents matching the filter.
func (c *Collection) Count(selector interface{}) (int64, error) {
if selector == nil {
selector = bson.D{}
}
var err error
var count int64
count, err = c.collection.CountDocuments(context.TODO(), selector)
return count, err
}