{
"_id" : "28847",
"title" : "Node即学即用",
"subtitle" : "",
"author" : "Mike Wilson",
"cate_id" : "223",
"list_order" : "6",
}
平常用Mysql比较多所以在查询时自然想到ORDER BY list_order DESC,
但是MongoDB并不能按照数字来排序字符串字段值,
上面list_order字符串值需要在个位数字前面补“0” 01,02,03,04,05,06,07,08,09,10,11 这样才能返回想要的排序结果。
list_order的值存储为整数类型
{
"_id" : "28847",
"title" : "Node即学即用",
"subtitle" : "",
"author" : "Mike Wilson",
"cate_id" : "223",
"list_order" : NumberLong(6),
}
注意:如果用字符串进行排序,那么比较的时候是按照ascii 来比较的, 所以位数不一样可能会出现异常情况。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有