-
Notifications
You must be signed in to change notification settings - Fork 3
/
aggregate.go
140 lines (119 loc) · 2.77 KB
/
aggregate.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
package bom
import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
// AggregateStages Stage data
type AggregateStages []StageInterface
// LookupStage find stage
type LookupStage struct {
from string
localField string
foreignField string
as string
}
// Aggregate method
func (as *AggregateStages) Aggregate() ([]primitive.M, error) {
l := len(*as)
result := make([]primitive.M, l)
for i, s := range *as {
result[i] = s.GetStage()
}
return result, nil
}
// GetStage get stage
func (l *LookupStage) GetStage() primitive.M {
return primitive.M{
LookupAggregateOperator: primitive.M{
"from": l.from,
"localField": l.localField,
"foreignField": l.foreignField,
"as": l.as,
},
}
}
// NewLookupStage constructor
func NewLookupStage(from, localField, foreignField, as string) *LookupStage {
return &LookupStage{
from: from,
localField: localField,
foreignField: foreignField,
as: as,
}
}
// MatchStage math stage cases
type MatchStage struct {
cases primitive.M
}
// GetStage stage getter
func (m *MatchStage) GetStage() primitive.M {
return primitive.M{
MatchAggregateOperator: m.cases,
}
}
// AddCondition add aggregation condition
func (m *MatchStage) AddCondition(key string, value interface{}) {
if m.cases == nil {
m.cases = primitive.M{}
}
cm := *m
cm.cases[key] = value
*m = cm
}
// NewMatchStage create stage
func NewMatchStage() *MatchStage {
return new(MatchStage)
}
// FacetStage create facet stage
type FacetStage struct {
conditions []primitive.M
}
// GetStage stage
func (fs *FacetStage) GetStage() primitive.M {
return primitive.M{
FacetAggregateOperator: primitive.M{
"result": fs.conditions,
"total": []primitive.M{
{
"$group": primitive.M{
"_id": nil,
"count": primitive.M{
"$sum": 1,
},
},
},
},
},
}
}
// SetLimit limit stage
func (fs *FacetStage) SetLimit(limit int32) {
fs.conditions = append(fs.conditions, primitive.M{LimitOperator: limit})
}
// SetSkip set skip
func (fs *FacetStage) SetSkip(skip int32) {
fs.conditions = append(fs.conditions, primitive.M{SkipOperator: skip})
}
// SetSort set sort
func (fs *FacetStage) SetSort(sort primitive.M) {
fs.conditions = append(fs.conditions, primitive.M{SortOperator: sort})
}
// NewFacetStage create facet stage
func NewFacetStage() *FacetStage {
return &FacetStage{
conditions: make([]primitive.M, 0),
}
}
// ProjectStage projection stage
type ProjectStage struct {
projects primitive.M
}
// GetStage get stage
func (p *ProjectStage) GetStage() primitive.M {
return primitive.M{
ProjectAggregateOperator: p.projects,
}
}
// NewProjectStage create project stage
func NewProjectStage(project primitive.M) *ProjectStage {
return &ProjectStage{projects: project}
}