使用查询的Cloud Firestore案例不敏感排序 [英] Cloud Firestore Case Insensitive Sorting Using Query

查看:159
本文介绍了使用查询的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

aaa

bbb

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

Now, what I want is something like following:

AAA

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)的一种实现方法是通过案例折叠。如果您只想支持现代浏览器版本,那么下面给出了一个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天全站免登陆