问题描述
如何不区分大小写地按给定字段对MongoDB集合进行排序?默认情况下,我在a-z之前得到A-Z.
How can I sort a MongoDB collection by a given field, case-insensitively? By default, I get A-Z before a-z.
我正在使用Java.
推荐答案
更新:截至目前,mongodb具有不区分大小写的索引:
Update:As of now mongodb have case insensitive indexes:
Users.find({})
.collation({locale: "en" })
.sort({name: 1})
.exec()
.then(...)
外壳:
db.getCollection('users')
.find({})
.collation({'locale':'en'})
.sort({'firstName':1})
更新:该答案已过期,3.4将具有不区分大小写的索引.请查看JIRA以获取更多信息 https://jira.mongodb.org/browse/SERVER-90
Update: This answer is out of date, 3.4 will have case insensitive indexes. Look to the JIRA for more information https://jira.mongodb.org/browse/SERVER-90
不幸的是,MongoDB尚不区分大小写的索引: https://jira.mongodb.org/浏览器/SERVER-90 ,任务已被推迟.
Unfortunately MongoDB does not yet have case insensitive indexes: https://jira.mongodb.org/browse/SERVER-90 and the task has been pushed back.
这意味着排序当前不区分大小写的唯一方法是实际创建一个特定的小写"字段,然后复制所讨论的排序字段的值(当然是小写)并对其进行排序.
This means the only way to sort case insensitive currently is to actually create a specific "lower cased" field, copying the value (lower cased of course) of the sort field in question and sorting on that instead.
这篇关于MongoDB中不区分大小写的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!