Mongodb以不区分大小写的方式排序 [英] Mongodb sort with case insensitive manner
本文介绍了Mongodb以不区分大小写的方式排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在mongodb作为数据库的nodejs(express)中的一个项目中,我的工作非常努力.当我使用sort()获取所有数据时,它以错误的方式返回数据,因此有办法按我期望的那样正确地设置其格式,如下所示: 如果我们在数据库中有三个记录:
I am struct very hard at one project in nodejs(express) with mongodb as database. When i get all data using sort() it returns data in wrong manner, so is there way to get it properly format as i am expecting as below: If we have three record in DB:
---------------------
id | Name | aga
---------------------
1 | atul | 21
---------------------
2 | Bhavik | 22
---------------------
3 | Jay | 25
我现在得到的是:
2,3,1系列数据
我期望的是: 1,2,3
What i expect is to come is: 1,2,3
这意味着忽略大小写,而无需添加新列即可进行排序.
It means is to ignore the case while sorting is it possible without adding new column.
推荐答案
您需要使用 collation
在这里带有locale: "en"
You need to use collation
here with locale: "en"
db.collection.find({}).collation({ locale: "en" }).sort({ name: 1 })
对于下面的文件
{ "_id" : 1, "name" : "Bhavik" }
{ "_id" : 2, "name" : "Jay" }
{ "_id" : 3, "name" : "atul" }
你会得到
{ "_id" : 3, "name" : "atul" }
{ "_id" : 1, "name" : "Bhavik" }
{ "_id" : 2, "name" : "Jay" }
这篇关于Mongodb以不区分大小写的方式排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文