Home > Net >  How to query field exist some document in firebase
How to query field exist some document in firebase

Time:02-18

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

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

CodePudding user response:

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.

  • Related