11

I am checking whether the Boolean field called attending exists, however I am not sure how to do that.

Is there a function such as .child().exists() or something similar that I could use?

firebaseFirestore.collection("Events") .document(ID) .collection("Users") .get() .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if(task.isSuccessful()) { for(QueryDocumentSnapshot document: task.getResult()){ attending = document.getBoolean("attending"); } } } }); 

9 Answers 9

5

You can do the following:

 if(task.isSuccessful()){ for(QueryDocumentSnapshot document: task.getResult()){ if (document.exists()) { if(document.getBoolean("attending") != null){ Log.d(TAG, "attending field exists"); } } } } 

From the docs:

public boolean exists ()

Returns true if the document existed in this snapshot.

Sign up to request clarification or add additional context in comments.

3 Comments

I think OP is asking about how to check the existence of a Field inside a document, not a document itself.
@DanielSaito Yes, but you cannot only retrieve a field alone. You can only retrieve a document, so this is the best way, get the document then check if the field is null.
yeah, but I think OP is talking after get the document, doing the doc[index].get(field) of a list of documents where some docs have this field and other don't, I think is a good example that converges to the same error that OP is struggling haha, sorry if I'm not very clear in my explanations.
4

What you're doing now is correct - you have to read the document and examine the snapshot to see if the field exists. There is no shorter way to do this.

Comments

0

Here is how you can achieve it or you may have resolved already, this for any one searches for solution.

As per the documentation:

CollectionReference citiesRef = db.collection("cities"); Query query = citiesRef.whereNotEqualTo("capital", false); 

This query returns every city document where the capital field exists with a value other than false or null. This includes city documents where the capital field value equals true or any non-boolean value besides null.

For more information : https://cloud.google.com/firestore/docs/query-data/order-limit-data#java_5

Comments

0

I use following boolean method may be someone else can

for(QueryDocumentSnapshot document: task.getResult()){ if(document.contains("attending")){ attending = document.getBoolean("attending"); } } 

Comments

0

I didn't find any solution so what I do is a try - catch (sorry Flutter / Dart, not java):

 bool attendingFieldExists = true; DocumentSnapshot documentSnapshot = // your Document Query here await FirebaseFirestore.instance .collection('collection') .doc(FirebaseAuth.instance.currentUser?.uid) .get(); // trying to get "attending" field will throw an exception // if not existing try { documentSnapshot.get("attending"); } catch (e) { attendingFieldExists = false; print("oops.. exception $e"); } 

Then you can adapt the code you want to apply depending on attendingFieldExists. I'm new to Flutter / Dart so not sure this is the best way to handle this.. but it works for me.

Comments

0

instead of documentSnapshot.get("attending"); do this documentSnapshot.ContainsField("attending");

If the field exists it will return true else returns false.

Comments

0

How about using:

QueryDocumentSnapshot<Map<String, dynamic>> data = 'some data'; if (data.data().containsKey(fieldName)) { // do something. } 

Comments

0

You can use hasOwnProperty

if (docSnap.exists()) { ... if (docSnap.data().hasOwnProperty('myField')) { ... } else { console.log("No such field!"); } } else { console.log("No such document!"); } 

Comments

-3
You can check firestore document exists using this, CollectionReference mobileRef = db.collection("mobiles"); await mobileRef.doc(id)) .get().then((mobileDoc) async { if(mobileDoc.exists){ print("Exists"); } }); 

1 Comment

of what I understood, OP is asking about a field inside a doc, not a doc itself, test if a field exists inside a doc. I have the same issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.