15

Like upper question, i want to get value of some field in firebase firestore instead of all document with DocumentSnapshot

like this in SQL SELECT col_1, col_2, col_3 FROM table_name

How can i do it?

Thank you for the help.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
gnùhp gnắhT
  • 163
  • 1
  • 1
  • 7

4 Answers4

28

The Cloud Firestore client-side SDKs always read and returns full documents. There is no way to read a subset of the fields in a document.

You can retrieve the entire document, and then process the DocumentSnapshot to just use the fields you're interested. But this means you're using more bandwidth than needed. If this is a regular occurrence for your app, consider creating a secondary collection where each document contains just the fields you're interested in.

Also see Doug's answer here (for Swift): How to access a specific field from Cloud FireStore Firebase in Swift

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
4

Firestore can read single field value. Firebase guides looks like didn't show these simple method.

For example:

[android]

String value = document.getString("col_1");


[node.js]

const value = doc.data().col_1
Mike Yan
  • 1,669
  • 2
  • 21
  • 27
  • 2
    Thanks @Mike Yan it works. Really strange that Firebase guide did not include it. – Ovais Chaudhry Apr 05 '18 at 09:00
  • 3
    you're still reading the entire document from Firestore, if that matters. You are consuming the bandwidth of the full document with this read on the client. – regretoverflow Nov 02 '18 at 03:18
  • Yes, but at least the client need not loop through the whole document to get 1 single field data. – Mike Yan Nov 02 '18 at 04:40
  • Animal animal = document.toObject(Animal); then animal.getName() or in js const animal = document.data(); then animal.name, use models! – cutiko Mar 30 '19 at 14:44
0

Firebase allows a few ways to query like an RDBMS database. And these are very handy too. Try the Where clause. It returns a single document but u can add multiple filters. Example: check for more

-2

try this code:

//set a global variable
dataList: Array<any>;


const dataCollection = firebase.firestore().collection('data');
dataCollection.get(query => {
query.forEach(docs => {
this.dataList.push({id: doc.id, data:doc.data()});
  })
})