Yes, it is possible to verify the presence of a document on Firestore without retrieving the entire document data using the exists()
method. This method checks if the document exists in the specified collection without retrieving its contents, which can be especially useful for checking if a document exists to avoid unnecessary read operations. Here's an example code snippet in JavaScript:
const docRef = db.collection('users').doc('user123');
docRef.get().then((doc) => {
if (doc.exists) {
console.log('Document data:', doc.data());
} else {
console.log('Document does not exist.');
}
});
In the above code, the get()
method retrieves the document. Then, the if
statement checks if the document exists using the exists()
method. If the document exists, its contents can be accessed using the data()
method. If the document does not exist, a message will be logged indicating that it does not exist.
Asked: 2021-09-10 11:00:00 +0000
Seen: 12 times
Last updated: Aug 07 '21