FiRestore:如何获取存在特定字段的所有文档 [英] Firestore: How to get all documents where a specific field exist
本文介绍了FiRestore:如何获取存在特定字段的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在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
字段提供空值,如下所示:
document1 = {
date: '2022-02-02',
name: 'x',
array: null,
}
通过这种方式,以下查询将完全正常工作:
db.collection('employees').where(`array`, '!=', null).get();
如果它是一个正在进行的项目,并且array
字段根本不存在,那么只需使用array
包含空值来更新现有文档。这是可能的,因为null is a supported data type in Firestore。
这篇关于FiRestore:如何获取存在特定字段的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文