从Firestore集合中获取文档ID – Python

huangapple go评论72阅读模式
英文:

Get the document IDs from a collections firestore - Python

问题

我正在尝试从 evtid 文档中获取文档标识符,使用以下脚本:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

# 使用服务帐户凭据。
cred = credentials.Certificate('mycredentials.json')

app = firebase_admin.initialize_app(cred)

db = firestore.client()

coll = db.collection("events").stream()

for doc in coll:
    print(f'{doc.id}')

但没有任何内容显示。如果我为 evtid 和子集合 users 中的特定文档放入一个标识符 -

coll = db.collection("events").document("someid").collection("users").stream()

然后它会获取到文档(user)。我不知道 evtid 中文档的标识符,我首先需要找到它们。

我做错了什么?

结构是这样的屏幕截图:

从Firestore集合中获取文档ID – Python

英文:

I have a nested collection/document Firestore DB:

/events/evtid/users/user
 Coll  Docs  Coll  Docs

I am trying to get the document ids from evtid documents using the below script:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

# Use a service account.
cred = credentials.Certificate('mycredentials.json')

app = firebase_admin.initialize_app(cred)

db = firestore.client()

coll = db.collection("events").stream()

for doc in coll:
    print(f'{doc.id}')

But nothing is presented. If I put an id for an especific document in evtid and the subcollection users ->

coll = db.collection("events").document("someid").collection("users").stream()

then it gets the documents (user). I don't know the ids for the documents in evtid and I first need to find them out.

What am I doing wrong?

A screenshot how the structure looks like
从Firestore集合中获取文档ID – Python

答案1

得分: 2

根据我在您的Firestore数据库快照中所见,显然在events集合中没有任何文档。

这是因为该集合中的文档ID是斜体的,通常意味着主文档已被删除,但其中的子集合仍然存在。因此,您可以从users子集合中检索文档,但无法从events集合中检索文档。

您能够看到已删除的文档ID是因为您的文档ID下只有users子集合,这导致Firebase控制台显示events集合的文档ID,以便您可以选择它们。

如果您想要在不干扰users子集合的情况下用新数据填充已删除的数据,您可以在users子集合上使用集合组查询,遍历这些结果,并使用类似以下方式编写父文档:

document.ref.parent.parent.set({ user: "Bob", age: 18 })

参考链接:

还请查看:

英文:

Based on what I can see in your Firestore database snapshot, apparently there are no documents in the events collection.

The reason for this is that the document IDs in that collection are in italics, which usually means that the main documents have been deleted, but the subcollections within them still exist. As a result, you can retrieve documents from the users subcollections, but not from the events collection.

The fact that you are able to see the deleted documents ids is because You just have users subcollection under document IDs, which causes the Firebase console to show the document IDs of the events collection, so that you can select them.

If you want to backfill that deleted data with new one without disturbing the users subcollection then you can use a collection group query on users subcollection, loop through those results, and write the parent document with something like

document.ref.parent.parent.set({ user: "Bob", age: 18 })

Reference :

Also check :

huangapple
  • 本文由 发表于 2023年6月16日 07:56:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定