通过文档ID查询集合

时间:2019-08-23 10:36:16

标签: firebase flutter dart google-cloud-firestore

我正在寻找一种通过文档ID而不是字段进行查询的方法。

它可能看起来像这样:

Firestore.instance.collection('tournaments').where(documentId, searchInput)

在这种情况下,searchInput是不完整的documentId。

我在Stackoverflow上看到了一个解决方案,但不是为了解决问题:https://stackoverflow.com/a/52252264/8539070

要记住的另一件事是,我试图显示与ID的某些部分匹配的集合中所有文档的列表。

谢谢您的帮助!

2 个答案:

答案 0 :(得分:5)

您还可以使用以下方式通过其ID获取多个文档:

Firestore.instance.collection('tournaments')..where(FieldPath.documentId, whereIn: myIdList).snapshots()

其中myIdList是列表

享受

答案 1 :(得分:1)

在Cloud Firestore中不可能有多个文档具有相同的文档ID,这就是为什么查询文档ID很有可能不是您要寻找的解决方案。

您可以简单地使用document method获取单个文档。这将返回一个DocumentReference,也可以让您get the document并收听snapshots

Firestore.instance.collection('tournaments').document(documentId).snapshots()