[英]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.