Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore select where is not null

I'm using firebase to manage my project and I cannot get to create a query with a where clause where some value is not null.

Example: I have a collection of employees. Each have a list of equipments as an object where the key is the equipment id and the value a color.

user = {     firstName: 'blabla',     lastName: 'bloblo',     equipments: {         123: 'blue',         124: 'red'     } } 

I would like to get all the employees who has a certain equipment in the equipments. Lets say 123.

It comes to Select * from Employees where equipments.123 is not null. I've tried:

firestore.collection('employees').where(`equipments.${equipmentId}`, '!=', null) 

but it's not working.

I can't seems to make it work. Can you help me.

like image 376
charles-emile begin lavigne Avatar asked Jan 27 '18 19:01

charles-emile begin lavigne


People also ask

How do I get specific data from firestore?

Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group. These queries can also be used with either get() or addSnapshotListener() , as described in Get Data and Get Realtime Updates.

How do I avoid null values in Cloud Firestore?

If you're explicitly setting the fields to null in your database, however, you'll get null values first, so if you want to avoid explicit null values you can do: [1]: Sparse in the sense that if the field is not present in the document, Cloud Firestore does not create an index entry in the index for that document/field.

How to query for values lower than \uf8ff in Firestore?

Firestore can do that if you pass a String to the where () function. So what you can do is query for values lower than \uf8ff. That's a very high code point in the Unicode range. Since it is after most regular characters in Unicode, this query will return everything that is of type String:

What's new in Cloud Firestore?

This means you can now query, for example, all documents in a "Projects" collection where the project's status field is not equal to the value "completed" On a similar note, Cloud Firestore also supports not-in queries, where you can query for documents where fields are not in a list of values.

How are access control information stored in Cloud Firestore?

Many apps store access control information as fields on documents in the database. Cloud Firestore Security Rules can dynamically allow or deny access based on document data: The resource variable refers to the requested document, and resource.data is a map of all of the fields and values stored in the document.


1 Answers

Update Sep 2020: v7.21.0 introduces support for not equals (!=) queries! That means you can now use the code bellow:

firestore.collection('employees').where(`equipments.${equipm‌​entId}`, '!=', null) 

Previous answer:

Firestore has no "not equal" operator. But looking at the logic, what you're trying to do is query for values which are String, and not null. Firestore can do that if you pass a String to the where() function.

So what you can do is query for values lower than \uf8ff. That's a very high code point in the Unicode range. Since it is after most regular characters in Unicode, this query will return everything that is of type String:

firestore.collection('employees').where(`equipments.${equipm‌​entId}`, '<', '\uf8ff') 

Or you can simply query for values higher than "" (empty String):

firestore.collection('employees').where(`equipments.${equipm‌​entId}`, '>', '') 
like image 131
Rosário Pereira Fernandes Avatar answered Oct 08 '22 13:10

Rosário Pereira Fernandes