如何从 Firebase 的集合中的 Firestore 文档中获取单个字段?
How to fetch a single Field from a Firestore document in a collection from Firebase?
我正在尝试仅获取存储在 Firestore 集合文档中的 Map 字段,而不是获取整个文档,这显然需要更多的时间和带宽。
这是我从地图字段中获取数据的方式,该字段定义在由特定用户的用户 ID 命名的文档中 -
DocumentSnapshot snap = await driversRef.doc(user!.uid).get();
email = ((snap.data() as Map)['profile'] as Map)['email'];
name = ((snap.data() as Map)['profile'] as Map)['name'];
mobile = ((snap.data() as Map)['profile'] as Map)['mobile'];
age = ((snap.data() as Map)['profile'] as Map)['age'];
address = ((snap.data() as Map)['profile'] as Map)['address'];
但正如上面所说,它是在代码的第一行获取整个文档。
有什么方法可以从 Firestore 文档中获取任何数据类型的特定字段?
I am trying to fetch only a Map field stored in a document of Firestore collection instead of fetching the whole document which is obviously more time taking and bandwidth-consuming.
你没有办法做到这一点。所有 Firestore 侦听器都在文档级别触发。这意味着您不能只从文档中获取 Map 字段的值。这是整个文档或什么都没有。这就是 Firestore 的工作方式,不幸的是,我们无法改变它。
Is there any way I can get just a particular field of any data type from a Firestore document?
没有。但是,如果您只想读取单个 属性 的值,那么您应该考虑将 only 和 属性 存储在文档中。或者将单个 属性 存储在 Realtime Database. This practice is called 中,这在 NoSQL 数据库中是一种很常见的做法。
我正在尝试仅获取存储在 Firestore 集合文档中的 Map 字段,而不是获取整个文档,这显然需要更多的时间和带宽。
这是我从地图字段中获取数据的方式,该字段定义在由特定用户的用户 ID 命名的文档中 -
DocumentSnapshot snap = await driversRef.doc(user!.uid).get();
email = ((snap.data() as Map)['profile'] as Map)['email'];
name = ((snap.data() as Map)['profile'] as Map)['name'];
mobile = ((snap.data() as Map)['profile'] as Map)['mobile'];
age = ((snap.data() as Map)['profile'] as Map)['age'];
address = ((snap.data() as Map)['profile'] as Map)['address'];
但正如上面所说,它是在代码的第一行获取整个文档。
有什么方法可以从 Firestore 文档中获取任何数据类型的特定字段?
I am trying to fetch only a Map field stored in a document of Firestore collection instead of fetching the whole document which is obviously more time taking and bandwidth-consuming.
你没有办法做到这一点。所有 Firestore 侦听器都在文档级别触发。这意味着您不能只从文档中获取 Map 字段的值。这是整个文档或什么都没有。这就是 Firestore 的工作方式,不幸的是,我们无法改变它。
Is there any way I can get just a particular field of any data type from a Firestore document?
没有。但是,如果您只想读取单个 属性 的值,那么您应该考虑将 only 和 属性 存储在文档中。或者将单个 属性 存储在 Realtime Database. This practice is called