Skip to content

Commit 21b14be

Browse files
feat(aggregation):
- extend without-key methods
1 parent f2f9282 commit 21b14be

File tree

2 files changed

+1517
-0
lines changed

2 files changed

+1517
-0
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// Copyright 2024 chenmingyong0423
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package aggregation
16+
17+
import (
18+
"time"
19+
20+
"github.com/chenmingyong0423/go-mongox/types"
21+
"go.mongodb.org/mongo-driver/bson"
22+
)
23+
24+
func SumWithoutKey(expression any) bson.D {
25+
return bson.D{{Key: types.AggregationSum, Value: expression}}
26+
}
27+
28+
func PushWithoutKey(expression any) bson.D {
29+
return bson.D{{Key: types.AggregationPush, Value: expression}}
30+
}
31+
32+
func AvgWithoutKey(expression any) bson.D {
33+
return bson.D{{Key: types.AggregationAvg, Value: expression}}
34+
}
35+
36+
func FirstWithoutKey(expression any) bson.D {
37+
return bson.D{{Key: types.AggregationFirst, Value: expression}}
38+
}
39+
40+
func LastWithoutKey(expression any) bson.D {
41+
return bson.D{{Key: types.AggregationLast, Value: expression}}
42+
}
43+
44+
func MinWithoutKey(expression any) bson.D {
45+
return bson.D{{Key: types.AggregationMin, Value: expression}}
46+
}
47+
48+
func MaxWithoutKey(expression any) bson.D {
49+
return bson.D{{Key: types.AggregationMax, Value: expression}}
50+
}
51+
52+
func AddWithoutKey(expression any) bson.D {
53+
return bson.D{{Key: types.AggregationAdd, Value: expression}}
54+
}
55+
56+
func MultiplyWithoutKey(expressions ...any) bson.D {
57+
return bson.D{{Key: types.AggregationMultiply, Value: expressions}}
58+
}
59+
60+
func SubtractWithoutKey(s string, start, length int64) bson.D {
61+
return bson.D{{Key: types.AggregationSubtract, Value: []any{s, start, length}}}
62+
}
63+
64+
func DivideWithoutKey(expressions ...any) bson.D {
65+
return bson.D{{Key: types.AggregationDivide, Value: expressions}}
66+
}
67+
68+
func ModWithoutKey(expressions ...any) bson.D {
69+
return bson.D{{Key: types.AggregationMod, Value: expressions}}
70+
}
71+
72+
func ArrayElemAtWithoutKey(expression any, index int64) bson.D {
73+
return bson.D{{Key: types.AggregationArrayElemAt, Value: []any{expression, index}}}
74+
}
75+
76+
func ConcatArraysWithoutKey(arrays ...any) bson.D {
77+
return bson.D{{Key: types.AggregationConcatArrays, Value: arrays}}
78+
}
79+
80+
func ArrayToObjectWithoutKey(expression any) bson.D {
81+
return bson.D{{Key: types.AggregationArrayToObject, Value: expression}}
82+
}
83+
84+
func SizeWithoutKey(expression any) bson.D {
85+
return bson.D{{Key: types.AggregationSize, Value: expression}}
86+
}
87+
88+
func SliceWithoutKey(array any, nElements int64) bson.D {
89+
return bson.D{{Key: types.AggregationSlice, Value: []any{array, nElements}}}
90+
}
91+
92+
func SliceWithPositionWithoutKey(array any, position, nElements int64) bson.D {
93+
return bson.D{{Key: types.AggregationSlice, Value: []any{array, position, nElements}}}
94+
}
95+
96+
func MapWithoutKey(inputArray any, as string, in any) bson.D {
97+
return bson.D{{Key: types.AggregationMap, Value: bson.D{
98+
{Key: types.AggregationInput, Value: inputArray},
99+
{Key: types.AggregationAs, Value: as},
100+
{Key: types.AggregationIn, Value: in},
101+
}}}
102+
}
103+
104+
func FilterWithoutKey(inputArray any, cond any, opt *types.FilterOptions) bson.D {
105+
d := bson.D{{Key: types.AggregationInput, Value: inputArray}, {Key: types.AggregationCondWithoutOperator, Value: cond}}
106+
if opt != nil {
107+
if opt.As != "" {
108+
d = append(d, bson.E{Key: types.AggregationAs, Value: opt.As})
109+
}
110+
if opt.Limit != 0 {
111+
d = append(d, bson.E{Key: types.AggregationLimit, Value: opt.Limit})
112+
}
113+
}
114+
return bson.D{{Key: types.AggregationFilter, Value: d}}
115+
}
116+
117+
func EqWithoutKey(expressions ...any) bson.D {
118+
return bson.D{{Key: types.AggregationEq, Value: expressions}}
119+
}
120+
121+
func NeWithoutKey(expressions ...any) bson.D {
122+
return bson.D{{Key: types.AggregationNe, Value: expressions}}
123+
}
124+
125+
func GtWithoutKey(expressions ...any) bson.D {
126+
return bson.D{{Key: types.AggregationGt, Value: expressions}}
127+
}
128+
129+
func GteWithoutKey(expressions ...any) bson.D {
130+
return bson.D{{Key: types.AggregationGte, Value: expressions}}
131+
}
132+
133+
func LtWithoutKey(expressions ...any) bson.D {
134+
return bson.D{{Key: types.AggregationLt, Value: expressions}}
135+
}
136+
137+
func LteWithoutKey(expressions ...any) bson.D {
138+
return bson.D{{Key: types.AggregationLte, Value: expressions}}
139+
}
140+
141+
func CondWithoutKey(boolExpr, tureExpr, falseExpr any) bson.D {
142+
return bson.D{{Key: types.AggregationCond, Value: []any{boolExpr, tureExpr, falseExpr}}}
143+
}
144+
145+
func IfNullWithoutKey(expr, replacement any) bson.D {
146+
return bson.D{{Key: types.AggregationIfNull, Value: []any{expr, replacement}}}
147+
}
148+
149+
func SwitchWithoutKey(cases []any, defaultCase any) bson.D {
150+
if len(cases) != 0 && len(cases)%2 == 0 {
151+
branches := bson.A{}
152+
for i := 0; i < len(cases); i += 2 {
153+
branches = append(branches, bson.D{{Key: types.Case, Value: cases[i]}, {Key: types.Then, Value: cases[i+1]}})
154+
}
155+
return bson.D{bson.E{Key: types.AggregationSwitch, Value: bson.D{bson.E{Key: types.Branches, Value: branches}, bson.E{Key: types.DefaultCase, Value: defaultCase}}}}
156+
}
157+
return bson.D{}
158+
}
159+
160+
func DateToStringWithoutKey(date any, opt *types.DateToStringOptions) bson.D {
161+
d := bson.D{bson.E{Key: types.Date, Value: date}}
162+
if opt != nil {
163+
if opt.Format != "" {
164+
d = append(d, bson.E{Key: types.Format, Value: opt.Format})
165+
}
166+
if opt.Timezone != "" {
167+
d = append(d, bson.E{Key: types.Timezone, Value: opt.Timezone})
168+
}
169+
if opt.OnNull != nil {
170+
d = append(d, bson.E{Key: types.OnNull, Value: opt.OnNull})
171+
}
172+
}
173+
return bson.D{{Key: types.AggregationDateToString, Value: d}}
174+
}
175+
176+
func DayOfMonthWithoutKey(date time.Time) bson.D {
177+
return bson.D{{Key: types.AggregationDayOfMonth, Value: date}}
178+
}
179+
180+
func DayOfMonthWithTimezoneWithoutKey(date time.Time, timezone string) bson.D {
181+
return bson.D{{Key: types.AggregationDayOfMonth, Value: bson.D{bson.E{Key: types.Date, Value: date}, bson.E{Key: types.Timezone, Value: timezone}}}}
182+
}
183+
184+
func DayOfWeekWithoutKey(date time.Time) bson.D {
185+
return bson.D{{Key: types.AggregationDayOfWeek, Value: date}}
186+
}
187+
188+
func DayOfWeekWithTimezoneWithoutKey(date time.Time, timezone string) bson.D {
189+
return bson.D{{Key: types.AggregationDayOfWeek, Value: bson.D{bson.E{Key: types.Date, Value: date}, bson.E{Key: types.Timezone, Value: timezone}}}}
190+
}
191+
192+
func DayOfYearWithoutKey(date time.Time) bson.D {
193+
return bson.D{{Key: types.AggregationDayOfYear, Value: date}}
194+
}
195+
196+
func DayOfYearWithTimezoneWithoutKey(date time.Time, timezone string) bson.D {
197+
return bson.D{{Key: types.AggregationDayOfYear, Value: bson.D{bson.E{Key: types.Date, Value: date}, bson.E{Key: types.Timezone, Value: timezone}}}}
198+
}
199+
200+
func YearWithoutKey(date time.Time) bson.D {
201+
return bson.D{{Key: types.AggregationYear, Value: date}}
202+
}
203+
204+
func YearWithTimezoneWithoutKey(date time.Time, timezone string) bson.D {
205+
return bson.D{{Key: types.AggregationYear, Value: bson.D{bson.E{Key: types.Date, Value: date}, bson.E{Key: types.Timezone, Value: timezone}}}}
206+
}
207+
208+
func MonthWithoutKey(date time.Time) bson.D {
209+
return bson.D{{Key: types.AggregationMonth, Value: date}}
210+
}
211+
212+
func MonthWithTimezoneWithoutKey(date time.Time, timezone string) bson.D {
213+
return bson.D{{Key: types.AggregationMonth, Value: bson.D{bson.E{Key: types.Date, Value: date}, bson.E{Key: types.Timezone, Value: timezone}}}}
214+
}
215+
216+
func WeekWithoutKey(date time.Time) bson.D {
217+
return bson.D{{Key: types.AggregationWeek, Value: date}}
218+
}
219+
220+
func WeekWithTimezoneWithoutKey(date time.Time, timezone string) bson.D {
221+
return bson.D{{Key: types.AggregationWeek, Value: bson.D{bson.E{Key: types.Date, Value: date}, bson.E{Key: types.Timezone, Value: timezone}}}}
222+
}
223+
224+
func AndWithoutKey(expressions ...any) bson.D {
225+
return bson.D{{Key: types.AggregationAnd, Value: expressions}}
226+
}
227+
228+
func OrWithoutKey(expressions ...any) bson.D {
229+
return bson.D{{Key: types.AggregationOr, Value: expressions}}
230+
}
231+
232+
func NotWithoutKey(expressions ...any) bson.D {
233+
return bson.D{{Key: types.AggregationNot, Value: expressions}}
234+
}
235+
236+
func ConcatWithoutKey(expressions ...any) bson.D {
237+
return bson.D{{Key: types.AggregationConcat, Value: expressions}}
238+
}
239+
240+
func SubstrBytesWithoutKey(stringExpression string, byteIndex int64, byteCount int64) bson.D {
241+
return bson.D{{Key: types.AggregationSubstrBytes, Value: []any{stringExpression, byteIndex, byteCount}}}
242+
}
243+
244+
func ToLowerWithoutKey(expression any) bson.D {
245+
return bson.D{{Key: types.AggregationToLower, Value: expression}}
246+
}
247+
248+
func ToUpperWithoutKey(expression any) bson.D {
249+
return bson.D{{Key: types.AggregationToUpper, Value: expression}}
250+
}
251+
252+
func ContactWithoutKey(expressions ...any) bson.D {
253+
return bson.D{{Key: types.AggregationContact, Value: expressions}}
254+
}

0 commit comments

Comments
 (0)