Firestore: Get document id from a Query result
Closed this issue ยท 4 comments
At the outset, excellent library. I had some intermittent problems integrating the official library, specifically in React Native, and was also finding the size to be a concern. So I started out on creating a minimal library myself that targeted the REST API when i found this library. Has way more features than i could have integrated and with a much, much cleaner codebase than i could have ever had.
I am currently using the Authlite
and Firestorelite
modules(and also Storagelite
soon) and its been working great thus far. Mostly simple gets and sets to a defined ref. I however have a usecase where i need to use a Query
to return back filtered results. My query:
const getGroupsForUser = async ( userId: string|undefined ) => {
const groupsRef = firestorelite.ref(`groups`);
const groupQuery = groupsRef.query({
where: [['members', 'contains', userId]],
});
const results = await groupQuery.run();
return results;
}
This returns back an array of Documents
. I wanted access to the document id for each Document
in the results. I haven't figured out how to get it. I can of course always have a property, say _key
and insert the id as a value when creating the document(which i have done in the past in the official libraries) but i thought ill just check to see if there is an inbuilt way that i might have missed. Perhaps something like how getting the id from an add
is already built-in and accessible via ref.id
on the results.
Issue-Label Bot is automatically applying the label feature_request
to this issue, with a confidence of 0.74. Please mark this comment with ๐ or ๐ to give our bot feedback!
Links: app homepage, dashboard and code for this bot.
Thank you very much, I appreciate the feedback!
I'm happy to hear that you love it.
Regarding the question. Yes, there is a built-in way to access metadata.
All metadata resides in the doc.__meta__
property (which is an object with multiple useful fields), you can read more about it in the reference.
The reason for using such a weird key is because keys that match the pattern __*__
are not allowed into firebase, so there is no fear of collision with other props set by the user.
Please let me know if this answered your question, and have a great weekend!
Thanks for the info and yes that does answer my doubts perfectly well. I did notice the __meta__
property and it did indeed have useful information including the document id/key. I assumed it was a private property intended only for internal usage because of the underscores (as is usually the norm i think).
The reason for using such a weird key is because keys that match the pattern * are not allowed into firebase, so there is no fear of collision with other props set by the user.
Thats useful information as well. I had no clue.
Glad I could help!
Please feel free to ask anything else!