-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
139 lines (109 loc) · 2.41 KB
/
pipeline.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
package gom
import "go.mongodb.org/mongo-driver/bson"
// PipeUnwind = create pipe for unwind arrays. To spesify, prefix the field with dollar sign ($)
func PipeUnwind(path string, showEmptyArrays bool) bson.M {
m := bson.M{
"$unwind": bson.M{
"path": path,
"preserveNullAndEmptyArrays": showEmptyArrays,
},
}
return m
}
// PipeMatch = create pipe for match filter.
func PipeMatch(filter *Filter) bson.M {
m := bson.M{
"$match": BuildFilter(filter),
}
return m
}
// PipeLookup = create pipe for lookup to another collection.
func PipeLookup(fromCollection, localField, foreignField, as string) bson.M {
m := bson.M{
"$lookup": bson.M{
"from": fromCollection,
"localField": localField,
"foreignField": foreignField,
"as": as,
},
}
return m
}
// PipeLimit = create pipe for limit aggregation.
func PipeLimit(limit int) bson.M {
m := bson.M{
"$limit": limit,
}
return m
}
// PipeSkip = create pipe for skip aggregation.
func PipeSkip(skip int) bson.M {
m := bson.M{
"$skip": skip,
}
return m
}
// PipeSort = create pipe for single sort aggregation.
func PipeSort(field string, asc bool) bson.M {
s := 1
if !asc {
s = -1
}
m := bson.M{
"$sort": bson.M{
field: s,
},
}
return m
}
// PipeSortMultiple = create pipe for multiple sort aggregation.
func PipeSortMultiple(sortParams ...PipeSortParams) bson.M {
sortM := bson.M{}
for _, p := range sortParams {
s := 1
if !p.Ascending {
s = -1
}
sortM[p.Field] = s
}
m := bson.M{
"$sort": sortM,
}
return m
}
// PipeProject = create pipe for project aggregation.
func PipeProject(project bson.M) bson.M {
m := bson.M{
"$project": project,
}
return m
}
// PipeSwitch = create pipe for switch condition.
func PipeSwitch(switchCase PipeSwitchParams) bson.M {
branches := []bson.M{}
for _, c := range switchCase.Cases {
branches = append(branches, bson.M{
"case": BuildFilter(c.Case),
"then": c.Then,
})
}
m := bson.M{
"$switch": bson.M{
"default": switchCase.Default,
"branches": branches,
},
}
return m
}
// PipeGroup = create pipe for group aggregation.
func PipeGroup(id string, fields bson.M) bson.M {
m := bson.M{
"_id": id,
}
for k, v := range fields {
m[k] = v
}
return bson.M{
"$group": m,
}
}