Skip to content

Commit

Permalink
Merge pull request #106 from pinguo/ft_iter41_merge-mongodb
Browse files Browse the repository at this point in the history
add pipone
  • Loading branch information
pinguo-guzhongzhi authored Sep 23, 2024
2 parents 096d56b + 718d471 commit 283693f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions adapter/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,35 @@ func handleOptions(query qmgo.QueryI, options ...bson.M) (qmgo.QueryI, error) {
opts[k] = v
}
}
sortDir := func(v interface{}) int {
switch rv := v.(type) {
case int32:
return int(rv)
case int:
return int(rv)
case int64:
return int(rv)
}
return 1
}
if sort, ok := opts["sort"]; ok {
switch sort.(type) {
case string:
query.Sort(sort.(string))
case []string:
query.Sort(sort.([]string)...)
case bson.M:
fmt.Println(sort)
sortFields := []string{}
for k, v := range sort.(bson.M) {
if sortDir(v) < 0 {
k = "-" + k
}
sortFields = append(sortFields, k)
}
if len(sortFields) > 0 {
query.Sort(sortFields...)
}
default:
log.Println(fmt.Errorf("invalid mongo sort:%#v", sort))
}
Expand Down

0 comments on commit 283693f

Please sign in to comment.