Firestore select where is not null

2019-01-12 07:14发布

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.

1条回答
smile是对你的礼貌
2楼-- · 2019-01-12 07:32

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}', '>', '')
查看更多
登录 后发表回答