Add sort to find

This commit is contained in:
George Suntres
2026-04-25 12:42:30 -04:00
parent 7c60f4c57b
commit 374b4b22c9
6 changed files with 27 additions and 13 deletions

View File

@@ -97,6 +97,8 @@ func mapToOp(v string) string {
switch v { switch v {
case "eq": case "eq":
return "$eq" return "$eq"
case "le":
return "$le"
case "includes": case "includes":
return "$regex" return "$regex"
default: default:

14
find.go
View File

@@ -11,6 +11,7 @@ type FindOptions struct {
Offset int64 `json:"offset"` Offset int64 `json:"offset"`
Limit int64 `json:"limit"` Limit int64 `json:"limit"`
Alias string `json:"alias"` Alias string `json:"alias"`
Sort [][]any `json:"sort"`
} }
// Find is used to fetch the first page of data. // Find is used to fetch the first page of data.
@@ -31,7 +32,14 @@ func (c *MongoClient) Find(ctx context.Context, database, name string, filter bs
pageSize := max(limit, c.Limit) pageSize := max(limit, c.Limit)
sort := bson.M{"_id": 1} sortOpts := commons.BsonDFromSlice(opts.Sort)
var sort bson.D
if len(sortOpts) == 0 {
sort = sortOpts
} else {
sort = bson.D{{Key: "_id", Value: 1}}
}
if err := c.DiscriminatorCheckAndApplyToFilter(ctx, name, filter); err != nil { if err := c.DiscriminatorCheckAndApplyToFilter(ctx, name, filter); err != nil {
return nil, err return nil, err
@@ -139,7 +147,9 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil
Filter: filter, Filter: filter,
}) })
pipeline := BuildPaginationPipeline(offset, finalLimit, f, nil) sort := commons.BsonDFromSlice(opts.Sort)
pipeline := BuildPaginationPipeline(offset, finalLimit, f, sort)
// 2. Query // 2. Query
cursor, err := collection.Aggregate(ctx, pipeline) cursor, err := collection.Aggregate(ctx, pipeline)

2
go.mod
View File

@@ -3,7 +3,7 @@ module git.gsuntres.com/general/mongo
go 1.25.0 go 1.25.0
require ( require (
git.gsuntres.com/general/commons v0.0.0-20260423193720-28dcfb9e4ce9 git.gsuntres.com/general/commons v0.0.0-20260425145405-80a923f150d3
git.gsuntres.com/general/events v0.0.0-20260424194951-506e91ff46a2 git.gsuntres.com/general/events v0.0.0-20260424194951-506e91ff46a2
git.gsuntres.com/general/sys v0.0.1 git.gsuntres.com/general/sys v0.0.1
github.com/go-viper/mapstructure/v2 v2.5.0 github.com/go-viper/mapstructure/v2 v2.5.0

12
go.sum
View File

@@ -2,12 +2,12 @@ dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA= dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
git.gsuntres.com/general/commons v0.0.0-20260423193720-28dcfb9e4ce9 h1:wbyyIEMTQqnfE/AfYlzpSwtqbfUW5RP9F6Tt84S7RMI= git.gsuntres.com/general/commons v0.0.0-20260423193720-28dcfb9e4ce9 h1:wbyyIEMTQqnfE/AfYlzpSwtqbfUW5RP9F6Tt84S7RMI=
git.gsuntres.com/general/commons v0.0.0-20260423193720-28dcfb9e4ce9/go.mod h1:S93xcBczrgN+gZU0JWkPRnTcAMvQuTp1ChyKkOd/I50= git.gsuntres.com/general/commons v0.0.0-20260423193720-28dcfb9e4ce9/go.mod h1:S93xcBczrgN+gZU0JWkPRnTcAMvQuTp1ChyKkOd/I50=
git.gsuntres.com/general/events v0.0.0-20260424123722-6d275bca7319 h1:HB3q3hrUOrfREV6eJLIZa++QEyw9i0l3FAGkhRqb5Xs= git.gsuntres.com/general/commons v0.0.0-20260425141140-13c75f62b178 h1:+4W5P1MoGF3cis8/HOHXbfAOVY/7UD59UWcMge9d450=
git.gsuntres.com/general/events v0.0.0-20260424123722-6d275bca7319/go.mod h1:IQEt0/YT7vYHLTmRhjpqdHgHhagIHZNaay83fndui7s= git.gsuntres.com/general/commons v0.0.0-20260425141140-13c75f62b178/go.mod h1:S93xcBczrgN+gZU0JWkPRnTcAMvQuTp1ChyKkOd/I50=
git.gsuntres.com/general/events v0.0.0-20260424131043-d6c514c3c113 h1:snNU8UgQ50E1Ud3+hGSJzMZG+Y2ILbcfyF+DEdKDVP0= git.gsuntres.com/general/commons v0.0.0-20260425145405-80a923f150d3 h1:OyKK8RNTKt3exIJjxLvuTd1198TUXyGxbdvKlcLS+DU=
git.gsuntres.com/general/events v0.0.0-20260424131043-d6c514c3c113/go.mod h1:IQEt0/YT7vYHLTmRhjpqdHgHhagIHZNaay83fndui7s= git.gsuntres.com/general/commons v0.0.0-20260425145405-80a923f150d3/go.mod h1:S93xcBczrgN+gZU0JWkPRnTcAMvQuTp1ChyKkOd/I50=
git.gsuntres.com/general/events v0.0.0-20260424131248-c1c5eac835df h1:Ouu8cSqVKJAiJTmyvxeJm6gRjQ7IAhgdbkXDx0SnnHM= git.gsuntres.com/general/events v0.0.0-20260424194951-506e91ff46a2 h1:nYzjFP7+foDz9GgGMkuiyMjKMm/N7S65zLU8TiTVqHQ=
git.gsuntres.com/general/events v0.0.0-20260424131248-c1c5eac835df/go.mod h1:IQEt0/YT7vYHLTmRhjpqdHgHhagIHZNaay83fndui7s= git.gsuntres.com/general/events v0.0.0-20260424194951-506e91ff46a2/go.mod h1:IQEt0/YT7vYHLTmRhjpqdHgHhagIHZNaay83fndui7s=
git.gsuntres.com/general/sys v0.0.1 h1:JpGG6HCkJrTaCICR09kURhMTIc+/s8yb0lHQjo/TDVI= git.gsuntres.com/general/sys v0.0.1 h1:JpGG6HCkJrTaCICR09kURhMTIc+/s8yb0lHQjo/TDVI=
git.gsuntres.com/general/sys v0.0.1/go.mod h1:OVs7w4/tJO1GT7cLIeEsb90LuZqH2xYIVQODI5P1GJs= git.gsuntres.com/general/sys v0.0.1/go.mod h1:OVs7w4/tJO1GT7cLIeEsb90LuZqH2xYIVQODI5P1GJs=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk= github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=

View File

@@ -5,7 +5,7 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/v2/mongo"
) )
func BuildPaginationPipeline(skip, limit int64, filter bson.M, sort bson.M) mongo.Pipeline { func BuildPaginationPipeline(skip, limit int64, filter bson.M, sort bson.D) mongo.Pipeline {
pipe := mongo.Pipeline{ pipe := mongo.Pipeline{
// 1. GLOBAL FILTER: Always filter first to use indexes // 1. GLOBAL FILTER: Always filter first to use indexes
{{Key: "$match", Value: filter}}, {{Key: "$match", Value: filter}},

View File

@@ -80,6 +80,8 @@ func TestUpdateSet_WithAudit(t *testing.T) {
after := onAudit_after.(bson.M) after := onAudit_after.(bson.M)
before["name"] = "** CHANGED USING UPDATE SET **" before["name"] = "** CHANGED USING UPDATE SET **"
delete(before, "updatedAt")
delete(after, "updatedAt")
AssertSubset(t, after, before, "Should have been equal") AssertSubset(t, after, before, "Should have been equal")
} else { } else {