如何在 mongoDB 中进行字母数字排序? [英] How to do alphanumeric sort in mongoDB?

查看:10
本文介绍了如何在 mongoDB 中进行字母数字排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的收藏.

{"userID" : "TR31"}
{"userID" : "TR1059"}
{"userID" : "TR1043"}

我想按升序或降序对这个文档进行排序,我试过这种方式 db.col.find({}).sort({"userID" : 1})db.col.find({}).sort({"userID" : -1}) 但没有运气.

I want to sort this document in an ascending or descending order, I tried this way db.col.find({}).sort({"userID" : 1}) and db.col.find({}).sort({"userID" : -1}) but no luck.

预期结果:

    {"userID" : "TR31"}
    {"userID" : "TR1043"}
    {"userID" : "TR1059"}

请指教.提前致谢.

推荐答案

使用 collat​​ionnumericOrdering 在 3.4 中设置为 true.

Use collation with numericOrdering set to true in 3.4.

类似的东西

db.col.find({}).sort({"userID" : 1}).collation( { locale: "en_US", numericOrdering: true });

这篇关于如何在 mongoDB 中进行字母数字排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆