简体   繁体   English

Firestore:通过 ID 从 CollectionGroup 获取文档

[英]Firestore: get document by ID from CollectionGroup

I want to get a snapshot of a Book by its document ID.我想通过文档 ID 获取一Book的快照。

My structure looks like this:我的结构如下所示:

           stores
          /      \
    Store1        Store2
      |               |
    books            books
    /   \           /  |  \
 Book1 Book2   Book3 Book4 Book5

( stores is a collection) stores是一个集合)

I tried with a collection group:我尝试了一个收藏组:

my_id = "XXXYYYZZZ" # I know that there is a book with this ID
firestore.Client.collection_group('books').where('id', "==", my_id)

But it looks for id field instead of document ID.但它查找id字段而不是文档 ID。

I tried also to read all Book documents and filter by ID in Python, but I have maaany books, so that's not an option.我还尝试阅读所有Book文档并按 Python 中的 ID 进行过滤,但我有很多书籍,所以这不是一个选项。

books = firestore.Client.collection_group('books').get()
book = [b.id for b in books if b.id == my_id]

And I cannot define a specific path to the document, because I have multiple stores.而且我无法定义文档的特定路径,因为我有多个商店。

What can I do?我能做什么?

If you want to query for a document ID, you can use the FieldPath.document_id() operator .如果要查询文档 ID,可以使用FieldPath.document_id() operator

So that'd look something like (not a Pythonista here):所以这看起来像(这里不是 Pythonista):

.where(FieldPath.document_id(), "==", my_id)

Also see:另见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM