对Firestore子集合中的文档进行通配符查询 [英] Wildcard queries for documents in Firestore subcollections
问题描述
我的Firestore数据库的组织方式如下:(col =收藏,doc =文档)
my Firestore database is organized as follows: (col = collection, doc = document)
-data (col)
-userid -> random number (doc)
-clients (col)
-clientid -> random number (doc)
-clientid -> random number (doc)
-userid(doc)
-clients (col)
-clientid -> random number (doc)
...
我需要读取特定"clientid"文档中的所有数据,只是要求用户插入其clientid随机数.
and I need to read all data inside a specific "clientid" document just asking the user to insert it's clientid random number.
我想知道是否可以像数据库规则那样使用通配符语法读取此数据:
I wonder if I can read this data with wildcard syntax like in the rules of database:
var clientid = "65486649466"
var clientdata = db.collection("data").doc({userid}).collection("clients").doc(clientid);
还是有另一种方法?谢谢
Or there is another way of doing this? Thanks
推荐答案
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
.
这篇关于对Firestore子集合中的文档进行通配符查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!