cbdeveloper
cbdeveloper

Reputation: 31485

Check if a document exists on Firestore without get() the full document data

So this is possible:

const docSnapshot = await firebase.firestore().collection("SOME_COL").doc("SOME_DOC").get();
console.log(docSnapshot.exists);

But it "downloads" the whole document just to check if it exists. And I'm currently working with some havier documents and I have a script where I just need to know if they exist, but I don't need to download them at that time.

Is there a way to check if a document exist without .get() and avoid downloading the document data?

Upvotes: 2

Views: 875

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83163

It seems you are using the JavaScript SDK. With this SDK there isn't any way to only get a subset of the fields of a document.

One of the possible solutions is to maintain another collection with documents that have the same IDs than the main collection documents but which only hold a very small dummy field. You could use a set of Cloud Functions to synchronise the two collections (Documents creation/deletion).


On the other hand, with the Firestore REST API, it is possible, with the get method, to define a DocumentMask which defines a "set of field paths on a document" and is "used to restrict a get operation on a document to a subset of its fields". Depending on your exact use case, this can be an interesting and easier solution.

Upvotes: 4

Related Questions