0

I have a user collection with different users, but the user have different groups A and B.

  • Users (Collection)
    1. A (Document)

      • User (Collection)
        1. Jake (Document)
          • friends (Collection)
            1. Justin (Document)
            2. Leo (Document)
        2. Tom (Document)
          • friends (Collection)
            1. Sam (Document)
            2. Jim 1(Document)
        3. ...
        4. ... and so on
    2. B (Document)

      • User (Collection)
        1. Justin (Document)
          • friends (Collection)
            1. Jake (Document)
            2. Leo (Document)
        2. Sam (Document)
          • friends (Collection)
            1. Tom (Document)
            2. Jim 2(Document)
        3. ...
        4. ... and so on

Now I want to access the friends of group A with a collectionGroup query, because there are hundreds of users and I only want to use one request to access the friends, because of costs. Is this stupid?

When using

const query = db.collectionGroup('friends').where('name', '==', 'Tim');
    museums.get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            console.log(doc.id, ' => ', doc.data());
        });
    });

I get the the Id of Jim 1 and two. But I only want to get the Id of Jim in group A.

How can I limit the access of the collectionGroup to group A.

I hope you can help me thnaks

MoDi
  • 66
  • 7

1 Answers1

0

Collection group queries consider documents in all collections with the given name. There is no way to limit queries to a certain path at the moment.

This common workaround for this limittion is to name the collections differently, so friends_A and friends_B. That way you can run a collection group query on friends_A only.


Update I recently found out that it is possible after all to search under a specific path. Have a look at Sam's excellent answer here: CollectionGroupQuery but limit search to subcollections under a particular document

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807