Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firestore选择不为null的位置_Javascript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript Firestore选择不为null的位置

Javascript Firestore选择不为null的位置,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我正在使用firebase管理我的项目,我无法使用where子句创建一个查询,其中某些值不为null 我有一群员工。每个都有一个设备列表作为对象,其中键是设备id和颜色值 user = { firstName: 'blabla', lastName: 'bloblo', equipments: { 123: 'blue', 124: 'red' } } 我想让所有在设备中有特定设备的员工。比如说123 它用于从Equipment.

我正在使用firebase管理我的项目,我无法使用where子句创建一个查询,其中某些值不为null

我有一群员工。每个都有一个设备列表作为对象,其中键是设备id和颜色值

user = {
    firstName: 'blabla',
    lastName: 'bloblo',
    equipments: {
        123: 'blue',
        124: 'red'
    }
}
我想让所有在设备中有特定设备的员工。比如说123

它用于从Equipment.123不为空的员工中选择*。我试过:

firestore.collection('employees').where(`equipments.${equipmentId}`, '!=', null)
但它不起作用


我似乎无法使它工作。您能帮助我吗。

更新2020年9月:引入了对不等于(
!=
)查询的支持! 这意味着您现在可以使用下面的代码:

firestore.collection('employees')。其中('equipment.${equipm‌​entId}`,'!=',null)

先前的答案:

Firestore没有“不相等”运算符。但从逻辑上看,您要做的是查询
String
,而不是
null
。如果将字符串传递给
where()
函数

因此,您可以查询低于
\uf8ff
的值。在Unicode范围内,这是一个非常高的代码点。由于它位于Unicode中的大多数常规字符之后,因此此查询将返回所有
字符串类型的内容:

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

FWIW由于Firestore索引是稀疏的[1],您可以执行以下操作:

firestore.collection('employees').orderBy(`equipments.${equipm‌​entId}`)
您将只获得设置了该字段的文档。但是,如果要在数据库中将字段显式设置为
null
,则首先会得到null值,因此如果要避免显式null值,可以执行以下操作:

firestore.collection('employees').orderBy('equipments.${equipm‌​entId}').startAfter(null);

[1] :稀疏,即如果文档中不存在该字段,则Cloud Firestore不会在该文档/字段的索引中创建索引项。

Cloud Firestore没有“不相等”运算符。正如@FrankvanPuffelen提到的,没有“不平等”操作符,但我想到了一个解决方法。你能试着使用
firestore.collection('employees')。where('devices.${equipmentId}','Oops…,我把它误读为“where not exists”。where exists确实经常是可行的。听起来像是一个答案@RosárioPereiraFernandes!:-)我只是在检查不存在的文档中的任何字段(名为e.g..exists的字段)如果它等于真,那么更简单的是>=”哇,我想知道我怎么没有想到这一点。谢谢@danmcgrath很遗憾,这个答案似乎不适用于有文档引用的字段。请注意
=
不在
中是:
两个查询运算符都不会匹配指定字段不存在的文档。
谢谢@rockwotj,
.startAfter(null)
正是我需要的!