是否可以在Firestore中将Where In与集合组查询一起使用? [英] Is it possible to use Where In with a Collection Group query in Firestore?

查看:30
本文介绍了是否可以在Firestore中将Where In与集合组查询一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中:

-Users(顶级收藏夹):
--- user1(doc):
---------- pets(子集合):
-------------- pet1(doc)
-------------- pet2(doc)
--- user2(doc):
---------- pets(子集合):
-------------- pet3(doc)
-------------- pet4(doc)

-Users(Top level collection):
---user1(doc):
----------pets(sub collection):
--------------pet1(doc)
--------------pet2(doc)
---user2(doc):
----------pets(sub collection):
--------------pet3(doc)
--------------pet4(doc)

如果我有一个ID列表: list("pet2","pet3","pet4)

If i have a List of ids: list("pet2", "pet3", "pet4)

是否可以执行类似的操作并获取DocumentsSnapshots列表?

Is there a way to do something like this and get a back a List of DocumentsSnapshots?

firestore.collectionGruop("pets")whereIn(FieldPath.documentId(),list)

它与根集合一起工作,但我不知道这对于集合组是否可能

It works with a root collection but i dont know if this is possible with a collection group

推荐答案

FieldPath.documentId()上的过滤器无法按您期望的方式运行时,收集组查询是唯一的地方.这是因为有关此令牌实际工作方式的一些详细信息.如果仍然尝试这样做,将会得到如下错误:

A collection group query is the only place where a filter on FieldPath.documentId() does not work the way you expect. That's because of some details about the way that this token actually works. If you try to this anyway, you will get an error like this:

无效的查询.当通过FieldPath.documentId()查询集合组时,提供的值必须导致有效的文档路径,但是'x'不是因为它具有奇数个段.

Invalid query. When querying a collection group by FieldPath.documentId(), the value provided must result in a valid document path, but 'x' is not because it has an odd number of segments.

如果要在集合组查询中对文档ID进行过滤,则需要将文档ID作为每个文档中字段的值存储.如果您使用名为"id"的字段,则可以像这样过滤该字段:

If you want to do a filter on document IDs in a collection group query, you will need to store the ID of the document as the value of a field in each document. If you use the field called "id", then you can filter on that field like this:

firestore
    .collectionGruop("pets")
    .whereIn('id', list)

这将给您一个不同的错误,表明您需要创建索引,并为您提供一个链接.创建该索引后(可能要花一些时间),您应该一切顺利.

This will give you a different error saying that you need to create an index, and give you a link to do so. After you create that index (it might take some time), you should be good to go.

另请参阅:如何在Cloud Firestore中使用文档ID执行集合组查询

这篇关于是否可以在Firestore中将Where In与集合组查询一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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