简体   繁体   English

Firestore 如何检索过去文档自动生成的 ID?

[英]Firestore How to retrieve document's autogenerated id in the past?

In Firestore, I have added a single document in a subcollection, I don't know its id because it was added in the past, but I know its path.在 Firestore 中,我在子集合中添加了一个文档,我不知道它的 ID,因为它是过去添加的,但我知道它的路径。 How to get its id?如何获取它的id? Should I save its id at the creation?我应该在创建时保存它的 id 吗?

Thank you谢谢

I don't know its id because it was added in the past, but I know its path .我不知道它的 id 因为它是过去添加的,但我知道它的 path

If with "I know its path" you refer to the Document path , then the Document ID is the last element of the path, in which elements are separated by a slash.如果“我知道它的路径”指的是Document path ,那么 Document ID 是路径的最后一个元素,其中元素用斜杠分隔。 For example rootCollectionName/parentDocId/parentCollectionName/docId .例如rootCollectionName/parentDocId/parentCollectionName/docId


If with "I know its path" you refer to the path of the subcollection , then you could query the subcollection and take the first document, since there is a single doc in the collection.如果使用“我知道它的路径”你指的是子集合的路径,那么你可以查询子集合并获取第一个文档,因为集合中只有一个文档。

For example with Dart:例如 Dart:

QuerySnapshot querySnapshot = await Firestore.instance.collection("rootCollectionName/parentDocId/parentCollectionName").get();
var documentID = querySnapshot.docs[0].id;

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

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