I have a Firebase Firestore database in which I have to get all the field's inside a document by document id.
How to get the all the field values (inside Green Box) by document id (Red box)
If you want to get that specific document you can try this
DocumentReference docRef = db.collection("projects").document("YOURDOCIDHERE");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
If you want get all documents by key order then you can try making a query like this.
docRef.orderByKey()
Check firebase documention for more info.
Related
I have a Firestore collection like this:
Take a look at kost collection. There are 4 docouments in it, and each document has the following fields: fasilitas, gender, harga, etc
For each document, I want to retrieve all the fields.
Now I have this:
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
DocumentReference docRef = firestore.collection("kost").document();
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
for (DocumentSnapshot ds:task.getResult()){
}
}
}
});
It's not even a correct code, task.getResult() is underlined:
foreach not applicable to type
'com.google.firebase.firestore.DocumentSnapshot
What's the correct way, then?
Your problems is in this code:
DocumentReference docRef = firestore.collection("kost").document();
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
Your docRef points to a new, empty document. What you want instead is to point to the entire collection as shown in the documentation on reading all documents in a collection:
db.collection("kost")
.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.d(TAG, "Error getting documents: ", task.getException());
}
}
});
I'm new to firebase and recently started working on this. Every tutorial I saw, is about how to listen to data change in cloud firestore. What if I want to fetch teh data just once and don't wanna set anyy listener on them. Example scenario is, just fetching the data user last login time or profile data of any user
The documentation has a dedicated section about getting data a single time for a query. Just use the get() method on the query you build:
db.collection("cities")
.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.d(TAG, "Error getting documents: ", task.getException());
}
}
});
Or, for a single document:
DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
I am try to import a single field value from a document.The problem here is code is not printing giving the error.I have check the code from firestore documentation too.
I tried using OnSuccessListener too,yet no result.
private FirebaseFirestore db,db1;
Map<String,Object > number=new HashMap<>();
db = FirebaseFirestore.getInstance();
DocumentReference docRef = db.collection("goals").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
String TAG="lol";
System.out.println("IT CAME Here") ;
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
number=document.getData();
name = document.getString("number");
} else {
Log.d(TAG, "No such document");
}
} else {
System.out.println("get failed with ");
Log.d(TAG, "get failed with ", task.getException());
}
}
});
Not even the print statements in the else loops are being printed.It seems like its skipping that block of code
I am new to Android and Firestore, I was able to retrieve data from Firestore without any problems.
But, I'm not sure how to use the reference.
Here's the data structure for person;
{ name=abc, gender=male, title=software engineer, company=abc, useraccount=com.google.firebase.firestore.DocumentReference#4878aac
}
So I get the useraccount-reference like this;
DocumentReference userAccountRef = doc.getDocumentReference("useraccount");
My question is how to retrieve information from user account by using this userAccountRef? I don't see any API to get a document by reference.
To get a document from a DocumentReference you call get() on it. From the Firebase documentation:
userAccountRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null && document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
How to search at firestore documents? if firestore collection contains certain document and if there has a string field at document named 'title'. How can i search specific title using firebase android api.
It is documented in the Docs here, in the last section of the page, titled Get multiple documents from a collection.
Firestore provides a whereEqualTo function to query your data.
Example code (from Docs):
db.collection("cities")
.whereEqualTo("capital", true) // <-- This line
.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 documents: ", task.getException());
}
}
});
I have use MutableLiveData for search the specific user's name .where i pass the user's name and check whether it is available in Firestore or not
Here is my code:-
public MutableLiveData<UsersModel> getSpecificUser(final String name) {
final MutableLiveData<UsersModel> usersData = new MutableLiveData<>();
db.collection("users")
.whereEqualTo("name", name)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot snapshot, #Nullable FirebaseFirestoreException e) {
if(e!=null || snapshot.size()==0){
Toast.makeText(activity, "User not found", Toast.LENGTH_SHORT).show();
}
for (DocumentChange userDoc : snapshot.getDocumentChanges()) {
UsersModel user = userDoc.getDocument().toObject(UsersModel.class);
if (user.name != null) {
if (userDoc.getType() == DocumentChange.Type.ADDED || userDoc.getType() == DocumentChange.Type.MODIFIED) {
usersData.setValue(user);
}
}
}
}
});
return usersData;
}