Omar Abdelazeem
Omar Abdelazeem

Reputation: 411

Firestore filtering data flutter

Does filtering documents from firestore like this costs by number of documents in collection or number of returned documents , let's say if I have 1000 documents in the collection and filter data like this to get 20 documents , it is considered 1000 reading or 20 reading ?

final ordersRef = firestore.collection("orders");
var query =
    ordersRef.snapshots().map((snapshot) => snapshot.docs.where((doc) {
          DateTime orderDate = DateTime.parse(doc["ordered_at"]);

          return unifyDateFormat(previousDay)
                  .isBefore(unifyDateFormat(orderDate)) &&
              unifyDateFormat(nextDay).isAfter(unifyDateFormat(orderDate));
        }));

Upvotes: 0

Views: 161

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50930

That'll cost 20 reads only. Only number of documents matched and returned in your query are charged. I don't see any limit implied on your query. If you think your query will match a lot of documents, you should use .limit() to limit the number of documents that should be returned.

FirebaseFirestore.instance
  .collection('users')
  .limit(2) // <--
  .get()
  .then(...);

You can read more about limiting in the documentation. It's explained in the Firestore billing section of the documentation as well.

Upvotes: 1

Related Questions