Firestore 选择不为空的地方 [英] Firestore select where is not null
问题描述
我正在使用 firebase 来管理我的项目,但我无法使用 where 子句创建查询,其中某些值不为空.
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.
示例:我有一组员工.每个都有一个设备列表作为对象,其中键是设备 ID,值是颜色.
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'
}
}
我想在设备中得到所有拥有某种设备的员工.让我们说 123.
I would like to get all the employees who has a certain equipment in the equipments. Lets say 123.
这里是 Select * from Employees ,其中 devices.123 不为空.我试过了:
It comes to Select * from Employees where equipments.123 is not null. I've tried:
firestore.collection('employees').where(`equipments.${equipmentId}`, '!=', null)
但它不起作用.
我似乎无法让它工作.你能帮我吗.
I can't seems to make it work. Can you help me.
推荐答案
2020 年 9 月更新: v7.21.0 引入了对不等于 (!=
) 查询的支持!这意味着您现在可以使用以下代码:
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.${equipmentId}`, '!=', null)
上一个答案:
Firestore 没有不等于"操作员.但是从逻辑上看,您要做的是查询String
而不是null
的值.Firestore 可以做到这一点,前提是您将字符串传递给 where()
函数.
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.
所以你可以做的是查询低于 uf8ff
的值.这是 Unicode 范围内的一个非常高的代码点.由于它位于 Unicode 中大多数常规字符之后,因此此查询将返回 String
类型的所有内容:
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.${equipmentId}`, '<', 'uf8ff')
或者您可以简单地查询高于"的值.(空String
):
Or you can simply query for values higher than "" (empty String
):
firestore.collection('employees').where(`equipments.${equipmentId}`, '>', '')
这篇关于Firestore 选择不为空的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!