如何在 Firebase 或 AngularFire 查询中执行字符串操作?

How to perform string manipulation in Firebase or AngularFire query?

我正在尝试在我的 Firestore 数据库中执行搜索(通过文本输入)。但是,我想操纵正在测试我的字符串的 Firestore 项目(即 trim、规范化、转换为小写等)。例如,假设我们有以下自定义验证器:

export class CustomValidator {
  static drink(afs: AngularFirestore) {
    return (control: AbstractControl) => {

      const stringToSearch = control.value.normalize('NFD').replace(/[\u0300-\u036f]/g, '').trim().toLocaleLowerCase();

      return afs.collection('MyFirestoreCollection', ref => ref.where('drink', '==', stringToSearch)).valueChanges().pipe(
        debounceTime(500), // wait 500ms delay before querying the server to avoid useless querying
        take(1),
        map(arr => arr.length ? { drinkAlreadyExists: false } : null ),
      );

    };
  }
}

假设我在搜索框中键入单词 "Café",由于以下代码行,将用于搜索 Firestore 数据的字符串将变为 "cafe"。

const stringToSearch = control.value.normalize('NFD').replace(/[\u0300-\u036f]/g, '').trim().toLocaleLowerCase();

如何对我正在搜索的 Firestore 数据(换句话说,对我的 Firestore "drink" 字段)执行相同的字符串操作,这样如果我在搜索框和 Firestore 中的条目是 "Café",是否会找到匹配项等等?

Cloud Firestore 没有字符串操作原语。您不能使用 Firestore 搜索除 exact 字符串匹配和 exact .

以外的任何内容

一些开发人员 integrate with Algolia 用于复杂的搜索。