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

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

问题描述

几天前,由于 firestore 显然具有更灵活的查询,我将我的应用程序帖子从实时数据库移到了 cloud firestore.我一直在修改我的代码以使用 firestore 作为新数据库.现在我遇到了一个问题.如何检索Posts"集合中包含特定命名文档的Likes"集合的每个文档?我不知道这是否有意义,所以这是一张展示问题的图片:

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() 实际上是一个token,指的是完全唯一的文档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天全站免登陆