且构网

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

对Firestore子集合中的文档进行通配符查询

更新时间:2023-02-14 09:54:30

Firestore客户端查询中没有通配符.您需要为查询所需的所有集合ID和文档ID提供准确的值.

There are no wildcards in Firestore client queries. You need to provide the exact values for all collection and document IDs needed to make the query.

如果您需要查询整个数据库中所有称为客户端"的子集合(包括嵌套在数据"下的子集合,以及其他地方),则可以改用

If you need to query across all subcollections called "clients" in the entire database (including those nested under "data", and elsewhere), you can instead use a collection group query to find those documents:

const query = db.collectionGroup("clients")
query.get()...

此查询将返回所有名为"clients"的子集合中的所有文档.不幸的是,您不能基于每个客户文档的文档ID来过滤此查询.为此,您还需要将客户文档ID写入每个文档中的字段.假设您已完成操作,并且字段名称称为"id":

This query will return all the documents among all the subcollections named "clients". Unfortunately, you can't filter this query based on the document ID of each client document. What you would have to do for this is also write the client document ID as a field in each document. Suppose you've done that, and the field name is called "id":

const query = db.collectionGroup("clients").where("id", "==", clientid)
query.get()...

这将为您提供所有客户端"子集合文档,其中"id"字段为 clientid .

This will give you all the "clients" subcollection documents where the "id" field is clientid.