且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firestore查询文档以字符串开头

更新时间:2023-02-21 19:17:16

你可以但它很棘手。您需要搜索大于或等于所需字符串且小于后继密钥的文档。



例如,要查找包含字段'foo'盯着'bar'你会查询:



。 ,'bas');

这实际上是一种我们在客户端实现中用于扫描匹配路径的文档集合的技术。我们的继任关键计算是由扫描仪正在寻找以当前用户ID开头的所有密钥。


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.

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

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

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.