Reputation: 7338
This code listen to the updates from the given query, and get all the documents immediately.
import { collection, query, where, onSnapshot } from "firebase/firestore";
const q = query(collection(db, "posts"), where("character_count", ">", 1));
onSnapshot(q, (querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.data());
});
});
I want to get all events from query "where character_count > 1", but without immediately getting all the documents from the collection posts
which could fetch a millions of documents...
Is it possible to do this in FireStore?
Upvotes: 0
Views: 45
Reputation: 317362
Firestore listeners will always get all documents matching the query in the first callback. There is no way to change that behavior. You can't tell it to only give you "new" documents.
If you want to introduce the concept of "new" documents, you will need to add a timestamp field to documents in the collection, and use that as a filter in the query. The problem is that the query will also have to provide a timestamp in the client app to use for filtering, and that timestamp is going to be dependent on the clock on that machine, which might not match the clock being used by the software that's adding the documents.
In short, Firestore isn't really designed to be a messaging service that lets clients understand what new "events" (documents) are passing through the system. There are other cloud products that do that, which are typically called message queues or pub/sub.
Upvotes: 1