0

I am using react-native to develop my application.

Now I want to get the document id for a document (id generated by the firestore) from one of my collections by querying it using one of the "unique" data stored in that document (like id, username, phone number, email etc.). What would be the fastest way to get this document id?

Is there a way which is faster than running a query like:

documentRef = db.collection('MYDOCS').where("tel","==","12345678").limit(1);
documentRef.get().then((querySnapshot)=>{
  documentID = querySnapshot.docs[0].id;
})

Thanks!

honor
  • 7,378
  • 10
  • 48
  • 76
  • 1
    You have to design your database stucture to fit your queries as much as possible : https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/ – MichelDelpech Apr 25 '19 at 19:07

1 Answers1

2

There is no faster way. You have to query for the document exactly as you're showing. There is no way to query for just the ID of the document - you must read and transfer the entire document.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    The Admin SDK has a way to select specific fields, but that is not available in the client-side SDKs. https://cloud.google.com/nodejs/docs/reference/firestore/1.2.x/CollectionReference#select Also see https://stackoverflow.com/questions/48903472/firestore-request-for-a-list-of-collection-ids and https://stackoverflow.com/questions/48488797/get-only-documents-names-in-firestore – Frank van Puffelen Apr 25 '19 at 19:18
  • Thanks for the answer Doug. And thanks for the comment @FrankvanPuffelen. Frank, I have read through the links you have shared. the first link (select) seems close to what I am looking for. but since it is in adminSDK, I think I will have to write some sort of a callable cloud function to utilize that, right? I think the second link is about COLLECTION IDs, not DOC IDs. In the third, "documents.list" does not seem to support some sort of "where" filtering which is what I need to get the DOC ID for a specific let's say USER document. – honor Apr 26 '19 at 06:33
  • A Cloud Function would indeed be one way to expose the functionality. You'd typically lose the realtime nature of the Firestore SDKs that way, so it's a trade-off you'll have to make. You could also have a collection of empty documents, with just their IDs btw. – Frank van Puffelen Apr 26 '19 at 14:04