简体   繁体   中英

How to get value of some field in firebase firestore android?

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.

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.

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

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. 例子: check for more

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()});
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM