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.
Related
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")
I want to change single field in all document of my collection. How I can do this in java ?
Structure of collection:
"Users"
== SCvm1SHkJqQHQogcsyvrYC9rhgg2 (user)
- "isPlaying" = false (field witch I want to change)
== IOGgfaIF3hjqierH546HeqQHhi30
- "isPlaying" = true
I tried use something like this but it's work
fStore.collection("Users").document().update("isPlaying", false);
I made research and found question about the same problem but that is in JavaScript which I don't understand. Thanks.
You can retrieve all documents in a collection using getDocuments() and then update each document. The complete code is :
//asynchronously retrieve all documents
ApiFuture<QuerySnapshot> future = fStore.collection("Users").get();
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
document.getReference().update("isPlaying", true);
}
To update a document, you must know the full path for that document. If you don't know the full path, you will need to load each document from the collection to determine that path, and then update it.
Something like:
db.collection("Users")
.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());
document.getReference().update("isPlaying", false);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
This code is mostly copy/paste from the documentation, so I recommend spending some more time there.
Firebase experts recommend using transaction https://firebase.google.com/docs/firestore/manage-data/transactions
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.
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
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());
}
}
});