使用查询的 Cloud Firestore 不区分大小写的排序 [英] Cloud Firestore Case Insensitive Sorting Using Query

查看:20
本文介绍了使用查询的 Cloud Firestore 不区分大小写的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 OrderBy 从 Cloud Firestore 读取排序数据.Firestore 按以下顺序返回数据:

I tried to read sorted data from Cloud Firestore using OrderBy. And Firestore returned data as Following Order:

AAA
BBB
啊啊
bbb

现在,我想要的是以下内容:

Now, what I want is something like following:

AAA
啊啊
BBB
bbb

我只希望使用 OrderBy 而非手动排序来获得此结果.
有没有办法在 Firestore 中进行这样的排序?


请为我提供解决方案.

I want this result only using OrderBy not by manual Sorting.
Is there any way to sort like this in Firestore?


Please provide me a solution for this.

提前致谢.

推荐答案

Cloud Firestore 中的排序区分大小写.没有使排序忽略大小写的标志.

Sorting in Cloud Firestore is case sensitive. There is no flag to make the sorting ignore the case.

实现您的用例的唯一方法是将该字段存储两次.

The only way to achieve your use-case is to store the field twice.

假设您的字段存储AAA"&'aaa' 被称为 myData.在您的客户端代码中,您需要存储名为 myData_insensitive 的第二个字段,您可以在其中存储不区分大小写的数据副本.

Let's say your field that stores 'AAA' & 'aaa' is called myData. In your client code you'll need to store a second field called myData_insensitive where you store a case-insensitive copy of the data.

DocA:
-> myData = 'AAA'
-> myData_insensitive = 'AAA'

DocB:
-> myData = 'aaa'
-> myData_insensitive = 'AAA'

DocC:
-> myData = 'BBB'
-> myData_insensitive = 'BBB'

DocD:
-> myData = 'bbb'
-> myData_insensitive = 'BBB'

现在您可以通过myData_insensitive查询和/或订购,但显示myData.

Now you can query and/or order by myData_insensitive, but display myData.

关于这个领域的两个有趣的事情是:

Two interesting thing about this area is:

  1. 使用 Unicode,删除大小写比toLowerCase"更复杂
  2. 不同的人类语言会对相同的字符进行不同的排序

无需为每个排序规则创建单独的索引来解决 (2),处理 (1) 的一种实现方法是通过 case 折叠.如果您只想支持现代浏览器版本,那么下面为您提供了一个 JavaScript 示例:

Without creating separate indexes for each collation to solve (2), one implementation approach to deal with (1) is via case folding. If you want to only support modern browser versions, then the following gives you a JavaScript example:

caseFoldNormalize = function (s){
  return s.normalize('NFKC').toLowerCase().toUpperCase().toLowerCase()
};
caseFoldDoc = function(doc, field_options) {
  // Case fold desired document fields
  if (field_options != null) {
    for (var field in field_options) {
      if (field_options.hasOwnProperty(field)) {
        switch(field_options[field]) {
          case 'case_fold':
            if (doc.hasOwnProperty(field) && Object.prototype.toString.call(doc[field]) === "[object String]") {
              doc[field.concat("_insensitive")] = caseFoldNormalize(doc[field])
            }
            break;
        }
      }
    }
  }
  return doc;
}

var raw_document = {
  name: "Los Angeles",
  state: "CA",
  country: "USA",
  structure: 'Waſſerſchloß',
  message: 'quıt quit' // Notice the different i's
};

var field_options = {
  name: 'case_fold',
  country: 'case_fold',
  structure: 'case_fold',
  message: 'case_fold'
}

var firestore_document = caseFoldDoc(raw_document, field_options);

db.collection("cities").doc("LA").set(firestore_document).then(function() {
  console.log("Document successfully written!");
}).catch(function(error) {
  console.error("Error writing document: ", error);
});

这将在 Cloud Firestore 中为您提供一个包含以下字段的文档:

This will give you a document in Cloud Firestore with the following fields:

{ 
 "name": "Los Angeles", 
 "state": "CA", 
 "country": "USA", 
 "structure": "Waſſerſchloß", 
 "message": "quıt quit", 
 "name_casefold": "los angeles", 
 "country_casefold": "usa", 
 "structure_casefold": "wasserschloss", 
 "message_casefold": "quit quit"
}

要处理旧浏览器,您可以在 如何使 toLowerCase() 和 toUpperCase() 跨浏览器保持一致

To handle older browser, you can see one solution in How do I make toLowerCase() and toUpperCase() consistent across browsers

这篇关于使用查询的 Cloud Firestore 不区分大小写的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆