Home > Web Front-end > JS Tutorial > body text

How Can I Achieve Case-Insensitive Sorting in Cloud Firestore?

Linda Hamilton
Release: 2024-10-22 08:52:02
Original
878 people have browsed it

How Can I Achieve Case-Insensitive Sorting in Cloud Firestore?

Cloud Firestore Case-Insensitive Sorting with Query

Cloud Firestore requires both queries and filters to be case-sensitive, leaving developers to seek alternative approaches for case-insensitive sorting. To address this, a popular technique involves creating duplicate fields while maintaining case-insensitive copies of the data.

Creating a Case-Insensitive Field

By creating a secondary field with a lowercase version of the original data, sorting and filtering can be performed based on this case-insensitive field. For example, a field called myData could have a corresponding myData_insensitive field with all values converted to lowercase.

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

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

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

DocD:
-> myData = 'bbb'
-> myData_insensitive = 'bbb'
Copy after login

Case-Folding for Unicode

Case-folding is recommended for Unicode support, as it handles complex character normalization and case-insensitive sorting. The following JavaScript snippet provides an example of how to implement case-folding:

<code class="javascript">caseFoldNormalize = function (s){
  return s.normalize('NFKC').toLowerCase().toUpperCase().toLowerCase()
};</code>
Copy after login

Sample Code

This code snippet demonstrates how to create a Firestore document with case-insensitive fields:

<code class="javascript">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);
});</code>
Copy after login

This will result in a document with both case-sensitive and case-insensitive fields:

<code class="javascript">{ 
 "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"
}</code>
Copy after login

Remember, while this technique offers a solution for case-insensitive sorting in Firestore, it requires extra storage space and incurs processing costs for every document update. As with any solution, consider the trade-offs and choose the approach that best aligns with your application requirements.

The above is the detailed content of How Can I Achieve Case-Insensitive Sorting in Cloud Firestore?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!