简体   繁体   English

如果您只知道一个字段(firestore)的数据,如何找到文档的id?

[英]How to find the id of a document if you only know the data of one field (firestore)?

I have a problem.我有个问题。 I have to get the automatically generated id of a document with the help of the data of a field which is in the document from which I have to find out the id.我必须借助文档中的字段数据获取自动生成的文档 ID,我必须从中找出 ID。

To make it more logical, an example: As you can see on the screenshot, I have the 'seller' collection, which in turn has different documents that contain different data.为了使它更合乎逻辑,举个例子:正如您在屏幕截图中看到的,我有“卖家”集合,它又包含包含不同数据的不同文档。 The app only knows what the name is, but not from which field it takes the name (eg name: 'Paul').该应用程序只知道名字是什么,但不知道它从哪个字段获取名字(例如名字:'Paul')。 The task is that the app now has to find out which document the name Paul comes from.任务是应用程序现在必须找出 Paul 这个名字来自哪个文档。 In this case it would be ' 4wHJZ3I2hAqbCFP0323A '.在这种情况下,它将是“4wHJZ3I2hAqbCFP0323A”。

I saw on the inte.net that there is a filter option for firestore, but I don't know how I can use this to get the ID of the document.我在 inte.net 上看到有 firestore 的过滤器选项,但我不知道如何使用它来获取文档的 ID。 Can anyone help me?谁能帮我?

在此处输入图像描述

You need to do the following:您需要执行以下操作:

final firestoreInstance = FirebaseFirestore.instance;
firestoreInstance
    .collection("seller")
    .where("name", isEqualTo: "Paul")
    .get()
    .then((value) {
  value.docs.forEach((result) {
    print(result.id);
  });
});

with async/await:使用异步/等待:

getData() async {
 final firestoreInstance = FirebaseFirestore.instance;
 final result = await firestoreInstance.collection("users").where("name", isEqualTo: "Paul").get();
 result.docs.forEach((result) {
    print(result.id);
  });
});

result.id will give you the document id where name = Paul . result.id将为您提供文档 ID,其中name = Paul

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

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