mongodb MongoDB中不区分大小写的排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22931177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Case insensitive sorting in MongoDB
提问by Varun Kumar
How can I sort a MongoDB collection by a given field, case-insensitively? By default, I get A-Z before a-z.
如何按给定字段对 MongoDB 集合进行排序,不区分大小写?默认情况下,我在 az 之前获得 AZ。
I'm using Java.
我正在使用 Java。
采纳答案by Sammaye
Update:As of now mongodb have case insensitive indexes:
更新:截至目前 mongodb 有不区分大小写的索引:
Users.find({})
.collation({locale: "en" })
.sort({name: 1})
.exec()
.then(...)
shell:
贝壳:
db.getCollection('users')
.find({})
.collation({'locale':'en'})
.sort({'firstName':1})
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
更新:此答案已过时,3.4 将具有不区分大小写的索引。查看 JIRA 以获取更多信息https://jira.mongodb.org/browse/SERVER-90
Unfortunately MongoDB does not yet have case insensitive indexes: https://jira.mongodb.org/browse/SERVER-90and the task has been pushed back.
不幸的是,MongoDB 还没有不区分大小写的索引:https: //jira.mongodb.org/browse/SERVER-90并且任务已被推迟。
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.
这意味着当前排序不区分大小写的唯一方法是实际创建一个特定的“小写”字段,复制有问题的排序字段的值(当然是小写)并对其进行排序。
回答by Neil Lunn
Sorting does work like that in MongoDB but you can do this on the fly with aggregate:
排序确实像在 MongoDB 中那样工作,但您可以使用聚合动态执行此操作:
Take the following data:
取以下数据:
{ "field" : "BBB" }
{ "field" : "aaa" }
{ "field" : "AAA" }
So with the following statement:
所以用下面的语句:
db.collection.aggregate([
{ "$project": {
"field": 1,
"insensitive": { "$toLower": "$field" }
}},
{ "$sort": { "insensitive": 1 } }
])
Would produce results like:
会产生如下结果:
{
"field" : "aaa",
"insensitive" : "aaa"
},
{
"field" : "AAA",
"insensitive" : "aaa"
},
{
"field" : "BBB",
"insensitive" : "bbb"
}
The actual order of insertion would be maintained for any values resulting in the same key when converted.
对于在转换时产生相同键的任何值,将保持实际插入顺序。
回答by Mladen Janjetovic
This has been an issuefor quite a long time on MongoDB JIRA, but it is solved now. Take a look at this release notes for detailed documentation. You should use collation
.
这个问题在 MongoDB JIRA 上已经存在很长时间了,但现在已经解决了。有关详细文档,请查看此发行说明。你应该使用collation
.
User.find()
.collation({locale: "en" }) //or whatever collation you want
.sort({name:1})
.exec(function(err, users) {
// use your case insensitive sorted results
});
回答by Monu Chaudhary
Adding the code .collation({'locale':'en'})
helped to solve my issue.
添加代码.collation({'locale':'en'})
有助于解决我的问题。
回答by Wajahath
As of now (mongodb 4), you can do the following:
截至目前(mongodb 4),您可以执行以下操作:
mongo shell:
蒙戈外壳:
db.getCollection('users')
.find({})
.collation({'locale':'en'})
.sort({'firstName':1});
mongoose:
猫鼬:
Users.find({})
.collation({locale: "en" })
.sort({name: 1})
.exec()
.then(...)
Here are supported languages and localesby mongodb.
以下是mongodb支持的语言和区域设置。
回答by Buzz Moschetti
Here it is in Java. I mixed no-args and first key-val variants of BasicDBObject
just for variety
这是在Java中。我混合了 no-args 和第一个 key-val 变体,BasicDBObject
只是为了多样性
DBCollection coll = db.getCollection("foo");
List<DBObject> pipe = new ArrayList<DBObject>();
DBObject prjflds = new BasicDBObject();
prjflds.put("field", 1);
prjflds.put("insensitive", new BasicDBObject("$toLower", "$field"));
DBObject project = new BasicDBObject();
project.put("$project", prjflds);
pipe.add(project);
DBObject sort = new BasicDBObject();
sort.put("$sort", new BasicDBObject("insensitive", 1));
pipe.add(sort);
AggregationOutput agg = coll.aggregate(pipe);
for (DBObject result : agg.results()) {
System.out.println(result);
}
回答by G Bojegowda
In Mongoose:-
在猫鼬中:-
Customer.find()
.collation({locale: "en" })
.sort({comapany: 1})
回答by SANJEEV RAVI
Tried all the above and answers Consolidating the result
尝试以上所有并回答 巩固结果
Answer-1:
答案-1:
db.collection.aggregate([
{ "$project": {
"field": 1,
"insensitive": { "$toLower": "$field" }
}},
{ "$sort": { "insensitive": 1 } } ])
Aggregate query converts the field into lower, So performance is low for large data.
聚合查询将字段转换为较低,因此对于大数据性能较低。
Answer-2:
答案 2:
db.collection.find({}).collation({locale: "en"}).sort({"name":1})
By default mongo follows uft-8 encoding(Z has high piriority then a) rules ,So overriding with language-specific rules. Its fast compare to above query Look into an official document to customize rules
默认情况下,mongo 遵循 uft-8 编码(Z 具有高优先级)规则,因此覆盖特定于语言的规则。与上面的查询相比,它的速度很快 查看官方文档以自定义规则
回答by Kevin Alviola
We solve this problem with the help of .sort function in JavaScript array
我们借助 JavaScript 数组中的 .sort 函数解决了这个问题
Here is the code
这是代码
function foo() { let results = collections.find({ _id: _id }, { fields: { 'username': 1, } }).fetch(); results.sort((a, b)=>{ var nameA = a.username.toUpperCase(); var nameB = b.username.toUpperCase(); if (nameA nameB) { return 1; } return 0; }); return results; }