如何在Cloud Firestore中使用文档ID执行集合组查询 [英] How to perform collection group query using document ID in Cloud Firestore

查看:108
本文介绍了如何在Cloud Firestore中使用文档ID执行集合组查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,由于Firestore显然具有更灵活的查询功能,因此我将我的应用程序帖子从实时数据库移至了Cloud Firestore.我一直在修改我的代码以将Firestore与新数据库一起使用.现在我遇到了一个问题.如何检索帖子"集合中具有喜欢"集合的每个文档,其中包含专门命名的文档?我不知道这是否有意义,所以下面的图片展示了问题所在:

So few day ago I moved my apps posts to cloud firestore from realtime database because of the more flexable querying that firestore apparently has. I have been modifying my code to work with firestore as the new database. Now I ran into a problem. How can I retrieve every document within "Posts" collection that has "Likes" collection that contains a specifically named document? I don't know if that makes sense so here's an image showcasing the problem:

我对Firebase实时数据库有一个有效的查询,但无法弄清楚如何使用Firestore进行查询.有可能还是必须更改数据库的结构?如果必须更改结构,获得相同结果的最便捷方法是什么?

I had a working query for firebase realtime database but can't figure out how to do it with firestore. Is it possible or do I have to change the structure of my database? If I do have to change the structure, what would be the most convenient way of getting the same result?

推荐答案

当前对于集合组查询是不可能的.您可能会想在查询FieldPath.documentId() == 'your-doucment-id'上放置一个过滤器,但这只是不受支持. FieldPath.documentId()实际上是一个令牌,它指向完全唯一的文档ID,其中包括文档的整个路径.

This is currently not possible with collection group queries. You might be tempted to put a filter on the query for FieldPath.documentId() == 'your-doucment-id', but that just isn't supported. FieldPath.documentId() is actually a token that refers to to fully unique document id, which includes the entire path of the document.

解决方法是将文档ID作为文档中的字段存储,然后根据该字段的内容进行过滤.因此,如果文档中有一个名为id的字段,带有文档ID,则可以查询collectionGroup("Likes").whereEquals('id', 'your-document-id').

The workaround is to store the document ID as a field in the document, and filter on the contents of that field instead. So, if the document had a field called id with the ID of the document, you could query for collectionGroup("Likes").whereEquals('id', 'your-document-id').

这篇关于如何在Cloud Firestore中使用文档ID执行集合组查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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