Skip to content

Commit

Permalink
mongo: Use ordered map for search parameters
Browse files Browse the repository at this point in the history
Mongo 1.5 requires the use of ordered maps when executing a sort
operation or it will panic.

changelog: none

Signed-off-by: Alf-Rune Siqveland <[email protected]>
  • Loading branch information
alfrunes committed Mar 24, 2021
1 parent d525aa3 commit da0e78e
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions store/mongo/datastore_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,9 @@ func (db *DataStoreMongo) GetDevices(ctx context.Context, q store.ListQuery) ([]
if q.Sort != nil {
name := fmt.Sprintf("%s-%s", q.Sort.AttrScope, model.GetDeviceAttributeNameReplacer().Replace(q.Sort.AttrName))
sortField := fmt.Sprintf("%s.%s.%s", DbDevAttributes, name, DbDevAttributesValue)
sortFieldQuery := bson.M{}
sortFieldQuery[sortField] = 1
sortFieldQuery := bson.D{{Key: sortField, Value: 1}}
if !q.Sort.Ascending {
sortFieldQuery[sortField] = -1
sortFieldQuery[0].Value = -1
}
findOptions.SetSort(sortFieldQuery)
}
Expand Down Expand Up @@ -955,13 +954,13 @@ func (db *DataStoreMongo) SearchDevices(ctx context.Context, searchParams model.
}

if len(searchParams.Sort) > 0 {
sortField := bson.M{}
for _, sortQ := range searchParams.Sort {
sortField := make(bson.D, len(searchParams.Sort))
for i, sortQ := range searchParams.Sort {
name := fmt.Sprintf("%s-%s", sortQ.Scope, model.GetDeviceAttributeNameReplacer().Replace(sortQ.Attribute))
field := fmt.Sprintf("%s.%s.%s", DbDevAttributes, name, DbDevAttributesValue)
sortField[field] = 1
sortField[i] = bson.E{Key: field, Value: 1}
if sortQ.Order == "desc" {
sortField[field] = -1
sortField[i].Value = -1
}
}
findOptions.SetSort(sortField)
Expand Down

0 comments on commit da0e78e

Please sign in to comment.