javascript - Firestore: Query multiple nested collections - Stack Overflow

I have a nested collection in firestore as shown below:db-devuseruser-id-1events-collection...user-id-

I have a nested collection in firestore as shown below:

db-dev
        user
             user-id-1
                       events-collection
                       ...
             user-id-2
                       events-collection
                       ...
             user-id-3
                       events-collection
                       ...

I want to query all the events from events-collection. However, I also want to keep the above schema as the admin needs to view all events-collection and specific users need to view their specific collection.

The query I write now looks something like this:

const uids = await getAllUserIds(); //Returns all user ID
const promises = uids.map(element => db.collection(`db-dev/user/${element}/events-collection/`).get())

const documents = await Promise.all(promises);

I am not very confident if this is the most optimized way to query the data or should I use something else? Can somebody please help me out here?

I have a nested collection in firestore as shown below:

db-dev
        user
             user-id-1
                       events-collection
                       ...
             user-id-2
                       events-collection
                       ...
             user-id-3
                       events-collection
                       ...

I want to query all the events from events-collection. However, I also want to keep the above schema as the admin needs to view all events-collection and specific users need to view their specific collection.

The query I write now looks something like this:

const uids = await getAllUserIds(); //Returns all user ID
const promises = uids.map(element => db.collection(`db-dev/user/${element}/events-collection/`).get())

const documents = await Promise.all(promises);

I am not very confident if this is the most optimized way to query the data or should I use something else? Can somebody please help me out here?

Share edited Feb 8, 2022 at 15:02 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges Recognized by Google Cloud Collective asked Feb 8, 2022 at 14:57 Shivam SahilShivam Sahil 4,9356 gold badges51 silver badges89 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

To read from all events-collection collections in one go, you can use a collection group query.

For example, without any conditions, this reads all documents from all collections named events-collection:

db.collectionGroup('events-collection').get()

If you want to read only the events-collection collections under /user, you can use the trick that Sam showed here: CollectionGroupQuery but limit search to subcollections under a particular document

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742402243a4437145.html

相关推荐

  • javascript - Firestore: Query multiple nested collections - Stack Overflow

    I have a nested collection in firestore as shown below:db-devuseruser-id-1events-collection...user-id-

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信