简体   繁体   English

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

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

相关问题 如何从 Firestore 获取所有文档的特定字段? - How to get specific fields for all documents from Firestore? 如何获取 firestore 集合中的所有文档? - How to get all documents in a collection in firestore? Firestore 查询,其中结果有一个字段,该字段包含要传递给第二个查询以检索所有文档的文档引用 - Firestore query where the results have a field that contains a document ref to be passed to a second query to retrieve all the documents Firestore expressjs:如何获取特定字段存在的文档并更新文档 - Firestore expressjs: how to get document where specific field exists and update the document 如何从 Firestore 获取所有文档并进行循环(云功能) - how to get all documents from Firestore and make a loop (Cloud Functions) 如何获取所有 Firestore Collections 和文档? | 反应JS - How to Get all Firestore Collections and Documents? | React JS 如何在 React Native 中从 Firestore 获取所有文档? - How to get All documents from Firestore in React Native? 如何从 Firestore 的集合中获取所有文档 - How to get all documents from a collection from Firestore 从 Firestore 集合中获取包含所有文档的列表 - Get a list with all documents from a Firestore collection 从 Firestore 数据库中获取所有文档 - Get all documents from Firestore Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM