Get data from Cloud Firestore - java

I need to get data from Firestore Firebase, I need to get the value of field "Company" from "employer" collection -> "TH17..." document. I try some things but it didnt work.
How I can do this?
I try this:
docref = db.collection("employer").document("TH17...");
docref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String p= document.getData().toString();
Log.d("TAG", "DocumentSnapshot data: " + document.getData());
}
}
}
});

I need to get the value of field "company" from "employer" collection -> "TH17..." document.
To solve this, please change the following lines of code:
String p= document.getData().toString();
Log.d("TAG", "DocumentSnapshot data: " + document.getData());
to
String company = document.getString("Company");
Log.d("TAG", "Company: " + company);
The result in your logcat will be:
Company: Test

Related

I am following the Firebase Assistant , But I am getting Errors with the given query to read data

I am trying to read data from the collection "dataToSave" , where field "Email" is equals to "test11#test11.com" , I am following the firebase assistant documentation but still getting an error.
db.collection("dataToSave")
.where("Email", "==", "test11#test11.com")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
// Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
// Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
I am expecting to see an output of all relevant data where email is equal to "test11#test11.com"
Image of where error is occuring
Use
db.collection("dataToSave").whereEqualTo("Email", "test11#test11.com")

How to get the id for document in collection?

how to get id for document a ?
const name=prod['categ'].value;
const a=db.collection('Categories').whereEqualTo("Name", name);
b=a.id;
You're not yet executing the query, which is necessary to get its ID.
Something like:
const query = db.collection('Categories').whereEqualTo("Name", name);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
The majority of that code is copied straight from the Firebase documentation on getting multiple documents from a collection, so I recommend spending some time studying that.

How do i List out Firebase Document wihtin a Collection?

I have a collection called 'Quiz' and its document contains Quiz category. How do I get all the category documents? Like only(Science,Technology..etc..)
View Image
I have tried with this code:
db.collection("Quiz")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("KKKK : ", document.getId() + " => " + document.getData());
}
} else {
Log.d("KKKK : ", "Error getting documents: ", task.getException());
}
}
});
But it never returns a value I have changed Firebase rule to allow read, write: if true;
If db object is defined as follows:
FirebaseFirestore db = FirebaseFirestore.getInstance();
To get all documents that exist within Questions subcollection, please use the following reference:
db.collection("Quiz").document("Science")
.collection("Questions")
.get()
.addOnCompleteListener(/* ... */);
See, you need to add all collection and document names in your reference, not only one, as it is in your actual code right now.

Getting all field names form a firestore document to an arraylist

I'm trying t get an ArrayList with all the field names from a document and convert it to an ArrayList.
I've been able to do this from a collection where I put all the documents in a ArrayList but I can't do it from a document.
Below is the code for all the documents from a colection and an image of the data base and what I want.
names_clinics= new ArrayList<>();
mFirebaseFirestore = FirebaseFirestore.getInstance();
mFirebaseFirestore.collection("CodeClinic")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
names_clinics.add(document.getId());
Log.d("CLINIC CODE", document.getId() + " => " + document.getData());
}
} else {
Log.d("CLINIC CODE", "Error getting documents: ", task.getException());
}
}
});
Thank you :D
To print those property names, please use the following code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference codesRef = rootRef.collection("CodeClinic").document("Codes");
codesRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
Map<String, Object> map = task.getResult().getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
list.add(entry.getKey());
Log.d("TAG", entry.getKey());
}
//Do what you want to do with your list
}
}
});
The output will be:
Clinica
FEUP
outra

Firestore - How Can I Get The Collections From a DocumentSnapshot?

Let's say I have a userSnapshot which I have got using get operation:
DocumentSnapshot userSnapshot=task.getResult().getData();
I know that I'm able to get a field from a documentSnapshot like this (for example):
String userName = userSnapshot.getString("name");
It just helps me with getting the values of the fields, but what if I want to get a collection under this userSnapshot? For example, its friends_list collection which contains documents of friends.
Is this possible?
Queries in Cloud Firestore are shallow. This means when you get() a document you do not download any of the data in subcollections.
If you want to get the data in the subcollections, you need to make a second request:
// Get the document
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
// ...
} else {
Log.d(TAG, "Error getting document.", task.getException());
}
}
});
// Get a subcollection
docRef.collection("friends_list").get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting subcollection.", task.getException());
}
}
});

Categories