Home > Software design >  How to get data from document in a parent collection based on a query in a subcollection
How to get data from document in a parent collection based on a query in a subcollection

Time:01-08

Hey guys I try to get data from a document which has a Field value Name and a subcollection named kurzwaffensub but for my project I need to do a collectionGroup query and after that I need the Name value of each document which matches the query from the subcollection.

My Current Structure

So let me explain. First I need to do a collectionGroup query of the documents from the subcollection based on their two parameter kurzHersteller and kurzModell which I marked in green at the picture. After that I get all Documents of every Subcollection which match the query.

And as you can see the blue document uid is the same uid as in the fieldvalue of every document of the subcollection.

And my goal is to get the red marked fieldvalue of the documents of the main collection after the group query of the subcollection.

But I only want to receive the Names of the documents which match the requirments of the query .

So in this Case i need the Name Felix Sturms because he has a document in his subcollection marked in yellow which match the Search for kurzHersteller : Andere and kurzKaliber : Andere

I dont know if this is possible or if I need to structure my data in another way. Im a beginner with firebase firestore so perhabs you can help me.


const [test, setTest] = useState([]);

const HEuKA = query(kurzRef,where("kurzHersteller", "==", `${kurzHersteller}` ),  where('kurzKaliber' , '==', `${kurzKaliber}`));

const handleClick = async () => {

...blablabla
...

} else if (kurzHersteller && kurzKaliber) {
        const modell = await getDocs(HeuKa);
        modell.forEach((doc) => {
  // doc.data() is never undefined for query doc snapshots
  setTest(doc.id, " => ", doc.data());
      
});
  } else { alert('Bitte etwas eingeben')}

  
}

So thats the first operation after I receive the array of the documents of the subcollection which match the query , i need another operation to get the corresponding documents from the parent collection which contain the information about the Name of the users which have a document in the subcollection which match the values kurzHersteller: Andere and kurzModell: Andere of this example.

CodePudding user response:

Once you get the QuerySnapshot from your collection group query, you can loop over every document and then access the parent document using .parent property present on the DocumentReference of each document. Try running the following code after your first query:

const modell = await getDocs(HeuKa);

const data = [];

for (const doc of modell.docs) {
  const parentDoc = await getDoc(doc.ref.parent.parent);
  const { Name } = parentDoc.data();
  data.push({
    ...doc.data(),
    Name,
  });
}

CodePudding user response:

Firestore reads/queries return documents from a single collection, or with collection group queries from all collections with the same name. There is no way in Firestore to include data from the parent document in the same read/query.

The two options you have are:

  1. Read the parent document for any result you get from the subcollection.
  2. Duplicate the necessary information from the parent document in each document in the subcollection.

While #1 is the most common option for people who are new to Firestore (and NoSQL databases in general), as you get more experienced with Firestore you'll often find yourself using #2 more often too.

CodePudding user response:

I think a more robust way, where you don't depend on the structure, is to get the document from 'uid' field you have on each document you get back from the query'. Even better would be to change this field to type "reference", in which case you can just do (assuming you create a field called customerReference as a replacement for uid):

 modell.forEach((doc) => {
   const name = doc.data().customerReference.get().Name;
  •  Tags:  
  • Related