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

前端 未结 4 1940
旧时难觅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 17:09

    Now Firestore allows the array-contains operator.
    If you want to filter documents which contain specific value, try this.

    First, change Map field to Array field.

    Post {
        title: ..
        ...
        categories: [
            cats,
            puppies
        ]
    }
    

    Second, use array-contains and orderBy for each different fields.

    let query = db.collection(`/posts`)
        .where('categories', 'array-contains', 'cats')
        .orderBy('createdAt')
        .startAfter(lastDate)
        .limit(5);
    

    You can check the official document about array-contains operator from here.

提交回复
热议问题