使用 firestore 仅查询特定字段

Query only specific field with firestore

我使用此代码从 Firestore 获取集合快照。

firestore().collection('project').where('userID', '==', authStore.uid).onSnapshot(onResult, onError);

这个returns数据量很大,但我只需要几个字段。是否可以只查询特定字段?例如,如果我只需要 projectNamecreationDate 字段。

Is it possible to query only a specific field?

不,那是不可能的。 Firestore 侦听器在文档级别触发。这意味着您将始终获得整个文档。

For example if I only need the projectName and the creationDate fields.

您不能只获取一组特定字段的值。这是整个文档或什么都没有。但是,如果您只需要读取这些值,仅此而已,那么您应该考虑将它们存储在一个单独的文档中。这种做法称为 ,这是 NoSQL 数据库的常见做法。

您还可以考虑对重复数据使用 Firebase Realtime Database