[英]Firestore: How to get all documents where a specific field exist
I have a collection in Firestore where some documents contain an array but some don't.我在 Firestore 中有一个集合,其中一些文档包含一个数组,但有些则没有。 The structure looks like this:
结构如下所示:
document1 = {
date: '2022-02-02',
name: 'x',
array: ['a'],
}
document2 = {
date: '2022-02-03',
name: 'y',
}
How can I get all documents where the field array
exists?如何获取字段
array
存在的所有文档? So in this example only document1?那么在这个例子中只有 document1?
Is any of these solutions possible:这些解决方案是否可行:
db.collection('employees').where(`array`, '!=', null).get();
db.collection('employees').where(`array`, '>=', []).get();
How can I get all documents where the field array exists?如何获取字段数组存在的所有文档?
When you add data to Firestore make sure to provide a null value for the array
field like this:当您将数据添加到 Firestore 时,请确保为
array
字段提供 null 值,如下所示:
document1 = {
date: '2022-02-02',
name: 'x',
array: null,
}
In this way, the following query will work perfectly fine:这样,下面的查询就可以完美地工作了:
db.collection('employees').where(`array`, '!=', null).get();
If it's an ongoing project, and the array
field doesn't exist at all, then simply update the existing documents with the array
holding a null value.如果它是一个正在进行的项目,并且
array
字段根本不存在,那么只需使用包含 null 值的array
更新现有文档。 This is possible because null is a supported data type in Firestore .这是可能的,因为null 是 Firestore 中支持的数据类型。
First of all you should try your solutions.首先,您应该尝试您的解决方案。
If they dont work, you can try to loop through your documents and check if doc.data().array
exists如果它们不起作用,您可以尝试遍历文档并检查
doc.data().array
是否存在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.