Ask Your Question
1

Is it possible to verify the presence of a document on Firestore without retrieving the entire document data?

asked 2021-09-10 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-08-07 21:00:00 +0000

huitzilopochtli gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-10 11:00:00 +0000

Seen: 12 times

Last updated: Aug 07 '21