I have a Firestore collection tree in which I plan to store only one document. I want to check if that collection contains that document and if so, retrieve the id of the document! Any ideas/thoughts?
Thanks :)
I have a Firestore collection tree in which I plan to store only one document. I want to check if that collection contains that document and if so, retrieve the id of the document! Any ideas/thoughts?
Thanks :)
If you want to check a particular document for existence based on its document id, please use the following lines of code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference docIdRef = rootRef.collection("yourCollection").document(docId);
docIdRef.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, "Document exists!");
} else {
Log.d(TAG, "Document does not exist!");
}
} else {
Log.d(TAG, "Failed with: ", task.getException());
}
}
});
If you don't have the document id, you need to use a query and find that document according to a value of a specific property like this:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference yourCollRef = rootRef.collection("yourCollection");
Query query = yourCollRef.whereEqualTo("yourPropery", "yourValue");
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() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
Try this one.
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("users").whereEqualTo("user_uid", user.getUid())
.limit(1).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
boolean isEmpty = task.getResult().isEmpty();
}
}
});
Make the document structure like this one:
collectionName(Collection) -> documentId(Document) -> id = documentId(and other fields)
Now make a query:
FirebaseFirestore firestoreDatabase= FirebaseFirestore.getInstance();
firestoreDatabase.collection(COLLECTION_NAME)
.whereEqualTo(DOCUMENT_ID, documentId)
.get()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
if (task.getResult().getDocuments().size() > 0)
// Here is your document with id
}
});
DocumentReference docRef = db.collection("items").document("your_id");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot snap, FirestoreException fe) {
if (snap.exists()) {
//update
} else {
//Insert
}
}
});
val rootRef: FirebaseFirestore = FirebaseFirestore.getInstance()
val userRef: CollectionReference = rootRef.collection("users")
userRef.get().addOnCompleteListener {
val response = Response()
if (it.isSuccessful) {
val result = it.result
result.let {
//Converts each response into user object to response data class
response.users = result.documents.mapNotNull {
it.toObject(User::class.java)
}
}
}
var userExists : Boolean = false
for (i in response.users!!){
//if users is found in db then set userExists to true
if (i.uid == (mAuth.currentUser?.uid)){
userExists = true
Toast.makeText(activity, "Welcome Back ${mAuth.currentUser?.displayName}", Toast.LENGTH_SHORT).show()
}
}
//if user not exists then save new user
if (!userExists){
Toast.makeText(activity, "New user saved", Toast.LENGTH_SHORT).show()
saveNewUser()
}
}