how to get Last Document id . give me example #8682
Replies: 8 comments 1 reply
-
What if you change |
Beta Was this translation helpful? Give feedback.
-
Ok. Can you provide a code snippet that reproduces the issue? This should include code that populates the database then tries to retrieve from it. I'd like to run this code locally to try and dig into the root cause. |
Beta Was this translation helpful? Give feedback.
-
Ideally, provide a git repo that I can clone and run. |
Beta Was this translation helpful? Give feedback.
-
Sorry . It seems I use another logic and no longer use this code logic . But until now I do not know how to do it . you can try it yourself with sample data Your own version. I just need to know what document id was last inputted. It's not in the Firestore documentation and no one on Google has discussed it yet. |
Beta Was this translation helpful? Give feedback.
-
The query that you show in the OP looks correct to get the last-added document. However, it assumes that you've correctly populated the "created_at" field with a server timestamp. Can you show me the code you use to add documents into the collection? |
Beta Was this translation helpful? Give feedback.
-
I have deleted the code. let's make it like this if for example without input create_at . like this tells me which document ID I last entered. give me the code example |
Beta Was this translation helpful? Give feedback.
-
I just closed this problem because I no longer use this logic in my project. If you still want to develop and add this function to your Firestore, please do so thanks |
Beta Was this translation helpful? Give feedback.
-
The screenshot does not show a "created_at" field in the document. There is no automatic "created_at" field in documents, you need to add it manually. When ordering by a field the query results implicitly omit all documents that lack that field, and, so, if none of the documents in the collection had a "created_at" field then the result set would be empty. In any case, since the issue is resolved for you I'll leave the issue as is. Feel free to resume the discussion in the future, if needed. |
Beta Was this translation helpful? Give feedback.
-
how to get last document id .The case is, if I didn't input it before, I want to know what the last document ID that was input was.
i try this code not work
const querySnapshot = await db.collection("Examplee") .orderBy("created_at") .limit(1) .get(); querySnapshot.forEach((doc) => { console.log("D",doc) });
Beta Was this translation helpful? Give feedback.
All reactions