-
Notifications
You must be signed in to change notification settings - Fork 1
/
bulk_model.go
100 lines (85 loc) · 3.12 KB
/
bulk_model.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
// Package mongo
package mongo
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// BulkWriteModel 批量写入模型
type BulkWriteModel struct {
models []mongo.WriteModel
ordered *bool
}
// NewBulkWriteModel 创建批量写入模型
func NewBulkWriteModel() *BulkWriteModel {
bulkWriteModel := new(BulkWriteModel)
return bulkWriteModel
}
func (bwm *BulkWriteModel) AddUpdateOneModel(filter *Query, upDoc map[string]interface{}, upsert bool) *BulkWriteModel {
updateOperation := bson.M{"$set": upDoc}
upModel := mongo.NewUpdateOneModel().SetFilter(filter.Cond()).SetUpdate(updateOperation).SetUpsert(upsert)
bwm.models = append(bwm.models, upModel)
return bwm
}
func (bwm *BulkWriteModel) AddUpdateManyModel(filter *Query, upDoc map[string]interface{}, upsert bool) *BulkWriteModel {
updateOperation := bson.M{"$set": upDoc}
upModel := mongo.NewUpdateManyModel().SetFilter(filter.Cond()).SetUpdate(updateOperation).SetUpsert(upsert)
bwm.models = append(bwm.models, upModel)
return bwm
}
// AddUpdateOneCustomModel 自定义类型包含:
func (bwm *BulkWriteModel) AddUpdateOneCustomModel(filter *Query, update updateType, upDoc map[string]interface{}, upsert bool) *BulkWriteModel {
updateOperation := bson.M{update.String(): upDoc}
upModel := mongo.NewUpdateOneModel().SetFilter(filter.Cond()).SetUpdate(updateOperation).SetUpsert(upsert)
bwm.models = append(bwm.models, upModel)
return bwm
}
func (bwm *BulkWriteModel) AddUpdateManyCustomModel(filter *Query, update updateType, upDoc map[string]interface{}, upsert bool) *BulkWriteModel {
updateOperation := bson.M{update.String(): upDoc}
upModel := mongo.NewUpdateManyModel().SetFilter(filter.Cond()).SetUpdate(updateOperation).SetUpsert(upsert)
bwm.models = append(bwm.models, upModel)
return bwm
}
func (bwm *BulkWriteModel) AddDeleteOneModel(filter *Query) *BulkWriteModel {
delModel := mongo.NewDeleteOneModel().SetFilter(filter.Cond())
bwm.models = append(bwm.models, delModel)
return bwm
}
func (bwm *BulkWriteModel) AddDeleteManyModel(filter *Query) *BulkWriteModel {
delModel := mongo.NewDeleteManyModel().SetFilter(filter.Cond())
bwm.models = append(bwm.models, delModel)
return bwm
}
func (bwm *BulkWriteModel) AddInsertOneModel(doc interface{}) *BulkWriteModel {
var m map[string]interface{}
switch doc := doc.(type) {
case map[string]interface{}:
m = doc
default:
m = Struct2Map(doc)
}
if id, ok := m["_id"]; ok {
switch id := id.(type) {
case string:
if id == "" {
delete(m, "_id")
} else {
m["_id"] = TryString2ObjectID(id)
}
}
}
delModel := mongo.NewInsertOneModel().SetDocument(m)
bwm.models = append(bwm.models, delModel)
return bwm
}
func (bwm *BulkWriteModel) AddNewReplaceOneModel(filter *Query, replaceDoc map[string]interface{}, upsert bool) *BulkWriteModel {
delModel := mongo.NewReplaceOneModel().SetFilter(filter.Cond()).SetReplacement(replaceDoc).SetUpsert(upsert)
bwm.models = append(bwm.models, delModel)
return bwm
}
func (bwm *BulkWriteModel) SetOrdered(ordered bool) *BulkWriteModel {
bwm.ordered = &ordered
return bwm
}
func (bwm *BulkWriteModel) Empty() bool {
return len(bwm.models) <= 0
}