我的 Firestore 数据库的组织方式如下: (col = 集合,doc = 文档)
-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 随机数。
我想知道是否可以像数据库规则一样使用通配符语法读取这些数据:
var clientid = "65486649466"
var clientdata = db.collection("data").doc({userid}).collection("clients").doc(clientid);
或者还有其他方法可以做到这一点吗? 谢谢
最佳答案
Firestore 客户端查询中没有通配符。您需要为进行查询所需的所有集合和文档 ID 提供准确的值。
如果您需要查询整个数据库中名为“clients”的所有子集合(包括嵌套在“data”下和其他位置的子集合),则可以使用 collection group query查找这些文档:
const query = db.collectionGroup("clients")
query.get()...
此查询将返回名为“clients”的所有子集合中的所有文档。遗憾的是,您无法根据每个客户端文档的文档 ID 来过滤此查询。为此,您还需要将客户文档 ID 作为字段写入每个文档中。假设您已经这样做了,并且字段名称称为“id”:
const query = db.collectionGroup("clients").where("id", "==", clientid)
query.get()...
这将为您提供所有“clients”子集合文档,其中“id”字段为 clientid
。
关于javascript - Firestore 子集合中文档的通配符查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60130643/