-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.go
65 lines (59 loc) · 2.02 KB
/
insert.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
package mongo
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
func returnDocument(document options.ReturnDocument) *options.ReturnDocument {
return &document
}
func (b Client) InsertOrReplace(database, collection string, filter bson.M, update interface{}) (bson.M, error) {
opts := &options.FindOneAndReplaceOptions{
Upsert: aws.Bool(true),
ReturnDocument: returnDocument(options.After),
}
col, err := b.GetCollection(database, collection, b.config.databaseOptions, b.config.collectionOptions)
if err != nil {
return nil, err
}
result := col.FindOneAndReplace(Ctx(), filter, update, opts)
if result.Err() != nil {
return nil, result.Err()
}
var resp bson.M
err = result.Decode(&resp)
return resp, err
}
func (b Client) InsertOne(database string, collection string, doc bson.M) (bson.M, error) {
col, err := b.GetCollection(database, collection, b.config.databaseOptions, b.config.collectionOptions)
if err != nil {
return nil, err
}
res, err := col.InsertOne(Ctx(), doc)
if err != nil {
return nil, err
}
if res == nil {
return nil, errors.New("result must not be nil")
}
id := res.InsertedID
doc, err = FindOne(col, bson.M{documentIDField: id})
return doc, err
}
func (b Client) ReplaceOne(database string, collection string, filter bson.M, replacement bson.A, opts ...*options.FindOneAndReplaceOptions) (bson.M, error) {
col, err := b.GetCollection(database, collection, b.config.databaseOptions, b.config.collectionOptions)
if err != nil {
return nil, err
}
result, err := FindOneAndReplace(col, filter, replacement, opts...)
return result, err
}
func (b Client) UpdateOne(database string, collection string, filter bson.M, update bson.A, opts *options.FindOneAndUpdateOptions) (bson.M, error) {
col, err := b.GetCollection(database, collection, b.config.databaseOptions, b.config.collectionOptions)
if err != nil {
return nil, err
}
result, err := FindOneAndUpdate(col, filter, update, opts)
return result, err
}