网站开发业务流程,免费书画网站怎么做的,如何申请空间,给你一个网站你怎么做的吗问题描述
MongoDB中有一个集合t_test_sort结构如下#xff0c;其中数值字段value为字符串类型#xff0c;现想按照value的数值大小进行降序排列。
{_id : ObjectId(656c87b36ca8100cd4a60348),name : 麻了,date其中数值字段value为字符串类型现想按照value的数值大小进行降序排列。
{_id : ObjectId(656c87b36ca8100cd4a60348),name : 麻了,date : 2033-04-05,value : -1.744353,_class : com.rkyao.spring.boot.mongo.entity.MongoSortEntity
}使用如下查询语句进行查询得到的结果显然不对这样是按照字符串进行排的而不是数值大小
db.getCollection(t_test_sort).find({}).sort({ value: -1 })尝试解决
网上搜索了一波解决方案都是要添加numericOrdering:true 配置这样可以将字符串按照数值排序但是试验后依然不行。
db.getCollection(t_test_sort).find({}).sort({ value: -1 }).collation({locale: zh,numericOrdering:true})百思不得其姐查询MongoDB官方文档后找到了原因原来是numericOrdering:true只支持非负整数负数和小数都不支持 网上的方案行不通只能继续研究了。
解决方案
方案一
把value字段的类型修改为数值型但是修改字段的影响比较大需要重新刷数据且有可能会影响其他业务所以除非走投无路暂不考虑。
方案二
再次查询官方文档后找到了一个方法可以在聚合管道中使用$toDecimal 将value字段的值转换为数值赋给一个临时字段sortField然后在根据sortField字段进行排序。
db.getCollection(t_test_sort).aggregate([{$addFields: {sortField: { $toDecimal: $value }}},{$sort: { sortField: -1 }}
])可以看到查询结果完全按照sortField数值大小降序排列问题解决。
Java代码也一并给出来
public ListMongoSortEntity sort() {ListAggregationOperation operationList new ArrayList();operationList.add(Aggregation.project(_id, name, date, value).and(ConvertOperators.ToDecimal.toDecimal($value)).as(sortField));operationList.add(Aggregation.sort(Sort.by(Sort.Direction.DESC, sortField)));Aggregation aggregation Aggregation.newAggregation(operationList);AggregationResultsMongoSortEntity results mongoTemplate.aggregate(aggregation, t_test_sort, MongoSortEntity.class);return results.getMappedResults();}参考文档
numericOrdering用法
https://www.mongodb.com/docs/manual/reference/collation/#std-label-numeric-order-restrictions
$toDecimal 用法
https://www.mongodb.com/docs/manual/reference/operator/aggregation/toDecimal/