2

may I know is there any way to access the Auto-Generate document ID in Firestore using Java? Because I want to add new field and data to existing document through my own android app. I have gone through the documentation, but didn't find any that solve the problem.

Elaboration of problem: I try to obtain the auto-generate ID of each "Event" document for the purpose of adding/update new field "Save Event" for each "User" in Firestore. So that 1 user can save many "event" inside the firestore.Code that I try to figure out DB structure

PI PI NAN
  • 59
  • 6
  • Hello there, please see if this [question](https://stackoverflow.com/questions/58213316/how-to-generate-and-guarantee-unique-values-in-firestore-collection) address your query on Firestore Auto Generated Ids. – Tonnie Aug 25 '21 at 04:37
  • If you already know the document ID then you can directly fetch that document. And, if you want to fetch all the documents and know their document IDs, you can use this [https://firebase.google.com/docs/firestore/query-data/get-data#get_all_documents_in_a_collection]. – Hitesh Patil Aug 25 '21 at 06:43
  • @Tonnie Thanks Tonnie, but I this post didn't answer how can I be able to obtain the Auto-Generate ID for existing document. I need a way to get the ID so that user can add a new field towards the existing document. – PI PI NAN Aug 25 '21 at 07:00
  • @HiteshPatil The problem is the ID is auto-generate, and I don't know how can I do to access the ID (once at a time). – PI PI NAN Aug 25 '21 at 07:05
  • Fetch all documents in the use `document.getId()`. If possible elaborate more about the issue or else attach your code to get an idea of what you have done and what you're trying to do. – Hitesh Patil Aug 25 '21 at 07:23
  • @HiteshPatil Hi, I already updated my problem on the post above. – PI PI NAN Aug 25 '21 at 08:02

1 Answers1

1

In order to use the document ID that you are looking for, first, you need to store it in a variable. When you are adding a document to the database and you are using a document() method call without passing any arguments, a unique ID is generated. To get that ID, you should use the following code:

String docId = collRef.document().getId();
collRef.document(docId).set(obj);

In which "collRef" is a reference that points to the collection in which you want to add the document, and the "obj" is the object that you want to be added to the database.

If needed, you can also store the document ID, as a property of your object.

Once you have this ID, you can perform the update like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference collRef = rootRef.collection("collName");
collRef.document(docId).update(map).addOnCompleteListener(/* ... /*);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Does it means that we need to capture the document ID into a variable on the time it is generated/created? If we don't do that, there is no way for us to get the particular ID again? – PI PI NAN Aug 25 '21 at 08:05
  • Yes, that's correct. You should store the ID of the document once it's created. Otherwise, you can't refer to a specific document if you don't have the corresponding ID. – Alex Mamo Aug 25 '21 at 08:08
  • Is there a way to pass certain identifier for each card view, so that when user click the cardView, user able to update new field in the specified document? – PI PI NAN Aug 26 '21 at 02:17
  • I'm not sure I completely understand your question. However, this question is not related to the initial one. So I recommend posting a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Aug 26 '21 at 07:08