How to get documents only have specified field value?

There are documents which includes user information.

"users" collection
 |- "user1" doc
    |- "name" : "John"
    |- "age" : 20
 |- "user2" doc
    |- "name" : "Mike"
    |- "age" : 30
 |- "user3" doc
    |- "name" : "Dave"
    |- "age" : 20
 |- "user4" doc
    |- "name" : "Chen"
    |- "age" : 25

What I want to do is getting names that have same age value. For example of above structure, when you want to get only 20 age, "John" and "Dave"'s documents returned.

Normally, you would get all the documents of "users" collection and choose them on the client, but this would waste Firebase's "Document reads" or help restrictions of network.

How can I make field selections on the Firebase side instead of on the client?

1 answer

  • answered 2021-10-24 17:52 Frank van Puffelen

    What you're looking for is a query. With this it'd look something like:

      .where('age', isEqualTo: 20)

    Also see the Firebase documentation on queries, which does not have Flutter/Dart examples but is more comprehensive aside from that.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum