Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to query field exist some document in firebase

I using firebase, nodejs and i have a question how to query field exist some document.

Example :

  Collections : users => document : A with field {
                                                    ....
                                                    is_correct: true
                                                   }
                          document : B with field {
                                                   .....
                                                  }

In my above example , i have two document in collection users. On document A i have field is_correct: true and on document B field is_correct not exist.
My collection users about 15.000 document and it have 50 document contain field is_correct: true

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When i write code look like :

await firestore.collection('users').where('is_correct', '==', true).get();

it can get correct me 50 document. But i don’t understand it can using index on field is_correct or not ? And it can query best performance ? I understand firebase can’t get document if field undefined. It impart in case ? Please help ? Thanks you

>Solution :

For a simple query like

firestore.collection('users').where('is_correct', '==', true)

you don’t need to configure any index, Firestore does it automatically.


As you mentioned, only documents where the given field exists can match the query.

And this is the case also for Not-equal (!=) and not-in queries: they exclude documents where the given field does not exist, as explained in the documentation.

Also, note that a field exists when it’s set to any value, including an empty string (""), null, and NaN (not a number).


So, in conclusion, if you want to query for documents where is_correct is not true, you need to create this field in these documents with a value different than true.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading