繁体   English   中英

Firestore:如何获取存在特定字段的所有文档

[英]Firestore: How to get all documents where a specific field exist

我在 Firestore 中有一个集合,其中一些文档包含一个数组,但有些则没有。 结构如下所示:

document1 = {
    date: '2022-02-02',
    name: 'x',
    array: ['a'],
}   

document2 = {
    date: '2022-02-03',
    name: 'y',
} 

如何获取字段array存在的所有文档? 那么在这个例子中只有 document1?

这些解决方案是否可行:

db.collection('employees').where(`array`, '!=', null).get();
db.collection('employees').where(`array`, '>=', []).get();

如何获取字段数组存在的所有文档?

当您将数据添加到 Firestore 时,请确保为array字段提供 null 值,如下所示:

document1 = {
    date: '2022-02-02',
    name: 'x',
    array: null,
}

这样,下面的查询就可以完美地工作了:

db.collection('employees').where(`array`, '!=', null).get();

如果它是一个正在进行的项目,并且array字段根本不存在,那么只需使用包含 null 值的array更新现有文档。 这是可能的,因为null 是 Firestore 中支持的数据类型

首先,您应该尝试您的解决方案。

如果它们不起作用,您可以尝试遍历文档并检查doc.data().array是否存在

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM