【问题标题】:How to perform string manipulation in Firebase or AngularFire query?如何在 Firebase 或 AngularFire 查询中执行字符串操作?
【发布时间】:2023-03-21 19:10:02
【问题描述】:

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

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”字段)执行相同的字符串操作,以便如果我在搜索框中键入单词“cafe”并且Firestore 中的条目是“Café”,会找到匹配项等等?

【问题讨论】:

    标签: angular firebase google-cloud-firestore angularfire


    【解决方案1】:

    Cloud Firestore 没有字符串操作原语。除了 exact 字符串匹配和 exact string prefixes 之外,您不能使用 Firestore 搜索任何内容。

    一些开发者integrate with Algolia 进行复杂的搜索。

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 2016-06-03
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多