Home > OS >  Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions
Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions

Time:08-10

I'm very new to Firebase cloud functions. Pardon me for my naive question. I have a Firestore Database in this format :

Collection(Products) => Document(unknownId) => Collection(Cheap) => Document(Soap) => Fields

My question is how do I get the fields of Soap without knowing its complete path. I have been fiddling around for last 2 days but no luck. I have tried many things but nothing seems to work, and I don't know which code to share here since I tried so many. I read the firebase cloud functions docs but it's more confusing.

exports.test = functions
  .https.onRequest((req, res) => {
      cors(req,res, async () => {
        admin.firestore().collectionGroup("Cheap").where(admin.firestore.FieldPath.documentId(), "==","Soap" ).get().then((result) => {
          console.log(result)
          res.status(200).send("Success") 
        }).catch((err) => {
          console.log('error')
        // }); 
      });
       
      })
});

CodePudding user response:

You can store the path you want to look for as a filed inside the document. In your case documentId(), "==","Soap", which is the relative path I'm assuming you need.

CodePudding user response:

The document ID field in a collection group query holds the entire path to the document, so your == on just the document name won't work in this context.

To allow the query, I'd probably store the value you want to query on in a field in the Cheap document too. So that means you'd give the Soap document a field Name: "Soap" too, and query on that field rather than the document ID.

  • Related