In Firestore, how can you do a compound query involving a key in a map without creating an index for every key?

前端 未结 4 1939
旧时难觅i
旧时难觅i 2021-02-01 16:26

In Firestore, how can you do a compound query involving a key in a map without creating an index for every key?

For example, consider a collection which holds blog posts

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 16:51

    This is doable by setting the value of each category to what you want to sort on. Firestore has a guide that covers this.

    Post {
        title: ..
        ...
        categories: {
            cats: createdAt
            puppies: createdAt
        }   
    }
    
    let query = db.collection(`/posts`)
        .where(`categories.${categoryId}`, '>', 0)
        .orderBy(`categories.${categoryId}`)
        .startAfter(lastDate)
        .limit(5);
    

提交回复
热议问题