Firestore 查询文档以字符串开头 [英] Firestore query documents startsWith a string

查看:25
本文介绍了Firestore 查询文档以字符串开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以查询 Firestore 集合以获取特定字符串开头的所有文档?

Is it possible to query a firestore collection to get all document that starts with a specific string?

我已经浏览了文档,但没有找到任何合适的查询.

I have gone through the documentation but do not find any suitable query for this.

推荐答案

你可以,但这很棘手.您需要搜索大于或等于您想要的字符串且小于后继键的文档.

You can but it's tricky. You need to search for documents greater than or equal to the string you want and less than a successor key.

例如,要查找包含以 'bar' 开头的字段 'foo' 的文档,您可以查询:

For example, to find documents containing a field 'foo' staring with 'bar' you would query:

db.collection(c)
    .where('foo', '>=', 'bar')
    .where('foo', '<', 'bas');

这实际上是我们在客户端实现中使用的一种技术,用于扫描与路径匹配的文档集合.我们的后继键计算scanner 调用> 正在查找以当前用户 ID 开头的所有键.

This is actually a technique we use in the client implementation for scanning collections of documents matching a path. Our successor key computation is called by a scanner which is looking for all keys starting with the current user id.

这篇关于Firestore 查询文档以字符串开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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