Skip to content

Commit 539a8b3

Browse files
heiytorotavio
authored andcommitted
refactor(api): use multiple $match stages in aggregation pipelines
- Refactor DeviceResolve and UserResolve to use pipeline-based query building - Change query options to append individual $match stages instead of modifying single match object - Move options processing after pipeline initialization to allow dynamic $match injection - Leverage MongoDB's automatic optimization of consecutive $match stages for better performance
1 parent dbb50e1 commit 539a8b3

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

api/store/mongo/device.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,6 @@ func (s *Store) DeviceResolve(ctx context.Context, resolver store.DeviceResolver
163163
matchStage["identity"] = bson.M{"mac": value}
164164
}
165165

166-
for _, opt := range opts {
167-
if err := opt(context.WithValue(ctx, "query", &matchStage)); err != nil {
168-
return nil, err
169-
}
170-
}
171-
172166
query := []bson.M{
173167
{
174168
"$match": matchStage,
@@ -207,6 +201,12 @@ func (s *Store) DeviceResolve(ctx context.Context, resolver store.DeviceResolver
207201
},
208202
}
209203

204+
for _, opt := range opts {
205+
if err := opt(context.WithValue(ctx, "query", &query)); err != nil {
206+
return nil, err
207+
}
208+
}
209+
210210
cursor, err := s.db.Collection("devices").Aggregate(ctx, query)
211211
if err != nil {
212212
return nil, FromMongoError(err)

api/store/mongo/query-options.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,33 @@ func (s *Store) Options() store.QueryOptions {
1515

1616
func (*queryOptions) InNamespace(tenantID string) store.QueryOption {
1717
return func(ctx context.Context) error {
18-
query, ok := ctx.Value("query").(*bson.M)
18+
query, ok := ctx.Value("query").(*[]bson.M)
1919
if !ok {
2020
return errors.New("query not found in context")
2121
}
2222

23-
(*query)["tenant_id"] = tenantID
23+
*query = append(*query, bson.M{
24+
"$match": bson.M{
25+
"tenant_id": tenantID,
26+
},
27+
})
2428

2529
return nil
2630
}
2731
}
2832

2933
func (*queryOptions) WithDeviceStatus(status models.DeviceStatus) store.QueryOption {
3034
return func(ctx context.Context) error {
31-
query, ok := ctx.Value("query").(*bson.M)
35+
query, ok := ctx.Value("query").(*[]bson.M)
3236
if !ok {
3337
return errors.New("query not found in context")
3438
}
3539

36-
(*query)["status"] = status
40+
*query = append(*query, bson.M{
41+
"$match": bson.M{
42+
"status": status,
43+
},
44+
})
3745

3846
return nil
3947
}

api/store/mongo/user.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,14 @@ func (s *Store) UserResolve(ctx context.Context, resolver store.UserResolver, va
121121
matchStage["username"] = value
122122
}
123123

124+
query := []bson.M{{"$match": matchStage}}
124125
for _, opt := range opts {
125-
if err := opt(context.WithValue(ctx, "query", &matchStage)); err != nil {
126+
if err := opt(context.WithValue(ctx, "query", &query)); err != nil {
126127
return nil, err
127128
}
128129
}
129130

130-
cursor, err := s.db.Collection("users").Aggregate(ctx, []bson.M{{"$match": matchStage}})
131+
cursor, err := s.db.Collection("users").Aggregate(ctx, query)
131132
if err != nil {
132133
return nil, FromMongoError(err)
133134
}

0 commit comments

Comments
 (0)