首页 > 解决方案 > 在字母或字符之前对数字进行排序

问题描述

mongodb有什么方法可以在字母或字符之前对数字进行排序吗?

例如:1, 2, 3, 4, - 我使用collation({locale: "en_US", numericOrdering: true})了它的输出 -, 1, 2, 3, 4。

但我预计 1、2、3、4,-

标签: mongodb

解决方案


custom-sorting 是您正在寻找的一种。

你可以这样做

 {
   $project:{
     "customFieldToBeCreatedForSorting":{
           $cond:[
             { $isNumber : "$FieldContainsMixedTypeData" },
             NumberLong(1111111111), //This can be customized by finding max in the previous stage
               $FieldContainsMixedTypeData
           ] 
     }
   }
 },
 {
    //$sort pipeline goes here
 }

推荐阅读