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

查看:769
本文介绍了如何在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"}

请告知.预先感谢.

推荐答案

使用 collation ,其中numericOrdering在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天全站免登陆