0

So I'm aware you can read through your own nested collections while having the following:

db.collection('usuarios').doc(user.uid).collection('pedidos').doc()

but that would only read all the "pedidos" of that current user

I'm making some kind of "admin" so I need to see ALL the "pedidos" of ALL users and if I remove the values of .doc(user.uid) it will not go through all users it will just not do anything

ej: db.collection("usuarios").doc().collection("estudiantes") <- This didn't work

So I'm looking for a way to read all users nested collection

I can read all users without issues is as simple as db.collection("usuarios") but I need to access their nested collection and I can't do something like db.collection("usuarios").collection("pedidos") that will give me an error. Any help is welcome

enter image description here

update based on suggestion

useEffect(() => { 
    const usuariosRef = db.collectionGroup('pedidos').get()
    usuariosRef.onSnapshot(snapshot => {
      const tempData = [];
      snapshot.forEach((doc) => {
        const data = doc.data();
        
        tempData.push(data);
      });
      setEstudiantes(tempData);
    })

}, [user]);

ReactPotato
  • 1,262
  • 3
  • 26
  • 46
  • 1
    You are probably looking for [Collection Group Queries](https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query). Checkout the linked docs and answer for more info. – Dharmaraj Oct 26 '21 at 18:06
  • I'll check into that – ReactPotato Oct 26 '21 at 22:57
  • @Dharmaraj I still can't see how to do what I want to do because in all the examples they do add a doc reference to access the querie and now the question is closed yay ... – ReactPotato Oct 27 '21 at 02:38
  • I do not have a document reference I just want to grab every single file on the nested collection such as db.collection('usuarios').doc().collection('pedidos') <- But I can't do this – ReactPotato Oct 27 '21 at 02:39
  • 1
    You must use `collectionGroup` instead of `collection` as in linked resources. `db.collectionGroup('pedidos').get()`. This will fetch all documents from pedidos collection of all usuarios. – Dharmaraj Oct 27 '21 at 04:35
  • but what about the first collection usuarios ? is it gonna skip it completly and go directly to "pedidos" ? I'm gonna give it a try it just doesn't make much sense on paper – ReactPotato Oct 27 '21 at 04:45
  • Also from what I'm seeing at the documentation it says .where( "something" == "something") doesn't that mean it will search for specific thing ? – ReactPotato Oct 27 '21 at 04:47
  • 1
    You want to fetch pedidos documents of all usuarios documents if I understand it correctly. That's what collectionGroup query will do. Just remove the `where()` since you are not filtering docs based on a field. Try copying the code from my previous comment? – Dharmaraj Oct 27 '21 at 04:54
  • yeah I'm on that was checking documentation first and had issues with the documentation one cause I don't code with await since I don't understand it and I tend to just avoid it – ReactPotato Oct 27 '21 at 04:57
  • let me post what I have atm cause is giving me an issue saying snapshot is not a function – ReactPotato Oct 27 '21 at 04:58
  • That's how I always get my data from collections so idk what I'm doing wrong – ReactPotato Oct 27 '21 at 04:59
  • 1
    Remove the `.get()` after collectionGroup... you never mentioned you were using a listener – Dharmaraj Oct 27 '21 at 05:01
  • Sorry I do not mention many things because I'm a potato at react literally started to learn this the bad way a few months ago imagine doing apps without 0 knowledge oof – ReactPotato Oct 27 '21 at 05:03
  • well that worked now I need to understand how, so basically what the term "collection group" does is to check between all the users any sub collection with the collection you assign in this case "pedidos" right ? – ReactPotato Oct 27 '21 at 05:04
  • Feel free to add an answer so I can give you your points, thanks for the help – ReactPotato Oct 27 '21 at 05:05
  • Question though can I use the same to delete and edit ? like db.collectionGroup('pedidos').delete() for example or db.collectionGroup('pedidos').update() for editing – ReactPotato Oct 27 '21 at 05:08
  • 1
    Glad to hear it is working. Question is closed so I can't answer but happy to help :) – Dharmaraj Oct 27 '21 at 05:08
  • ah okay welp thumbs up – ReactPotato Oct 27 '21 at 05:09
  • 1
    You can't update/delete docs without getting a reference to them. There are some related questions which you can look into but you can post a new question if none of them answer yours – Dharmaraj Oct 27 '21 at 05:09
  • I'll try to find them otherwise I'll be probably posting tomorrow – ReactPotato Oct 27 '21 at 05:10

0 Answers0