0

I need to access a single field in a firebase document without downloading the entire doc.

Usecase: I have a comments page and a comments collection in firebase. I need to know how many comments are in there without downloading them all. There is a doc with a field commentCount, which is updated every time a new comment is posted, and an array of all comments. I want to access the field commentCount and read its value.

this.angularFireStore.firestore.collection('comments/' + id + '/commentCount').get().then(col => {
      console.log(col);
    });

I have tried this, but it returns an undefinded. How can i achieve this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ping pong
  • 225
  • 6
  • 16
  • Cloud Firestore always reads a complete document. There is no way to read just a single field from a document. See https://stackoverflow.com/a/47477449/209103 – Frank van Puffelen Sep 15 '19 at 13:48

1 Answers1

0

My assumption is that commentCount is not a list, therefore it has to be Document and not a Collection.

Try this:

this.angularFireStore.firestore.document('comments/' + id).valueChanges()
 .subscribe(comment => console.log(comment.commentCount));

Also, I wouldn't place the comment count in the your comments list, what happens if your delete this specific comment?

TheUnreal
  • 23,434
  • 46
  • 157
  • 277