Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get documents where a specific field exists/does not exists in Firebase Cloud Firestore?

In Firebase Cloud Firestore, I have "user_goals" in collections and goals may be a predefined goal (master_id: "XXXX") or a custom goal (no "master_id" key)

In JavaScript, I need to write two functions, one to get all predefined goals and other to get all custom goals.

I have got some workaround to get custom goals by setting "master_id" as "" empty string and able to get as below:

db.collection('user_goals')     .where('challenge_id', '==', '')  // workaround works     .get() 

Still this is not the correct way, I continued to use this for predefined goals where it has a "master_id" as below

db.collection('user_goals')     .where('challenge_id', '<', '')  // this workaround     .where('challenge_id', '>', '')  // is not working     .get() 

Since Firestore has no "!=" operator, I need to use "<" and ">" operator but still no success.

Question: Ignoring these workarounds, what is the preferred way to get docs by checking whether a specific field exists or does not exists?

like image 515
Ram Babu Avatar asked Mar 30 '18 18:03

Ram Babu


People also ask

How do I get specific data from firestore?

There are three ways to retrieve data stored in Cloud Firestore. Any of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data once. Set a listener to receive data-change events.

Does firestore Create collection if not exists?

Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.

What is document reference in firebase?

A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location. The document at the referenced location may or may not exist. A DocumentReference can also be used to create a CollectionReference to a subcollection.

How do I get most recent documents on firestore?

Is there any way to get the last created document in Firebase Firestore collection? Yes, there is! The simplest way to achieve this is to add a date property to each object in your collection, then simply query it according to this new property descending and call limit(1) function. That's it!


2 Answers

As @Emile Moureau solution. I prefer

.orderBy(`field`) 

To query documents with the field exists. Since it will work with any type of data with any value even for null.

But as @Doug Stevenson said:

You can't query for something that doesn't exist in Firestore. A field needs to exist in order for a Firestore index to be aware of it.

You can't query for documents without the field. At least for now.

like image 120
hisoft Avatar answered Oct 05 '22 22:10

hisoft


The preferred way to get docs where a specified field exists is to use the:

.orderBy(fieldPath) 

As specified in the Firebase documentation:

enter image description here

Thus the answer provided by @hisoft is valid. I just decided to provide the official source, as the question was for the preferred way.

like image 38
Jarosław Wiśniewski Avatar answered Oct 05 '22 21:10

Jarosław Wiśniewski