简体   繁体   English

使用 firestore 仅查询特定字段

[英]Query only specific field with firestore

I use this code to get a collection snapshot from Firestore.我使用此代码从 Firestore 获取集合快照。

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

This returns a huge amount of data, but I only need a few fields.这将返回大量数据,但我只需要几个字段。 Is it possible to query only a specific field?是否可以只查询特定字段? For example, if I only need the projectName and the creationDate fields.例如,如果我只需要projectNamecreationDate字段。

Is it possible to query only a specific field?是否可以只查询特定字段?

No, that is not possbile.不,那是不可能的。 A Firestore listener fires on the document level. Firestore 侦听器在文档级别触发。 This means that you'll always get the entire document.这意味着您将始终获得整个文档。

For example if I only need the projectName and the creationDate fields.例如,如果我只需要projectNamecreationDate字段。

You cannot only get the value of a specific set of fields.您不能只获取一组特定字段的值。 It's the entire document or nothing.这是整个文档或什么都没有。 If you, however, only need to read those values and nothing more, then you should consider storing them in a separate document.但是,如果您只需要读取这些值,仅此而已,那么您应该考虑将它们存储在一个单独的文档中。 This practice is called denormalization , and it's a common practice when it comes to NoSQL databases.这种做法称为反规范化,在涉及 NoSQL 数据库时,这是一种常见的做法。

You might also take into consideration using the Firebase Realtime Database , for the duplicated data.您还可以考虑使用Firebase Realtime Database来处理重复数据。

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

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