mongodb - 在字母或字符之前对数字进行排序
问题描述
mongodb有什么方法可以在字母或字符之前对数字进行排序吗?
例如:1, 2, 3, 4, - 我使用collation({locale: "en_US", numericOrdering: true})
了它的输出 -, 1, 2, 3, 4。
但我预计 1、2、3、4,-
解决方案
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
}
推荐阅读
- node.js - 给定表示目录结构的 json 文件,如何使用节点自动创建目录结构?
- java - 如何发送地图
使用 Spring MVC 中的 @PathVariable - gitlab-ci - 如何创建被评估为 AND 而不是 OR 的 Gitlab CI 规则
- angular - 如何根据用户单击父组件中的对象在共享服务的子组件中显示数据?
- typescript - 如何修复'未找到规则'@typescript-eslint/no-use-before-declare'的定义。eslint(@typescript-eslint/no-use-before-declare)'
- mysql - 无法进入 Laravel 5.3 中的引用页面
- xml - S4s-att-not-allowed:属性“targetNameSpace”不能出现在元素“模式”验证错误中
- java - bris.platform.core.Registry.activateMasterTenant(Registry.java:626) [coreserver.jar:?
- reactjs - 如何访问表单内的道具
- laravel - 我正在尝试从发票中返回用户和项目数据