MongoDB中不区分大小写的排序

编程入门 行业动态 更新时间:2024-10-22 23:24:44
本文介绍了MongoDB中不区分大小写的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何不区分大小写地按给定字段对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以获取更多信息 jira.mongodb/browse/SERVER-90

Update: This answer is out of date, 3.4 will have case insensitive indexes. Look to the JIRA for more information jira.mongodb/browse/SERVER-90

不幸的是,MongoDB尚不区分大小写的索引: jira.mongodb/浏览器/SERVER-90 ,任务已被推迟.

Unfortunately MongoDB does not yet have case insensitive indexes: jira.mongodb/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中不区分大小写的排序

本文发布于:2023-10-16 12:02:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小写   中不   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!