简体   繁体   English

如何计算 Firestore 操作的读/写?

[英]How to calculate read/writes on Firestore operation?

I already know that having a direct path to a document id would result in a single read from the firestore through using the get() function. I am trying to retrieve a document fields value so I use FirebaseFirestore.instance.collection('users').doc(userUid).get() to get the documentSnapshot, then use await _userCollection.then((value) => value.get("field name")) to get the document field value.我已经知道,通过使用 get() function,拥有文档 ID 的直接路径将导致从 firestore 进行一次读取。我正在尝试检索文档字段值,所以我使用FirebaseFirestore.instance.collection('users').doc(userUid).get()获取文档快照,然后使用await _userCollection.then((value) => value.get("field name"))获取文档字段值。 I do this to get "n" fields.我这样做是为了获得“n”个字段。

So, my question is would the second get(),used to retrieve each document field, be calculated in the read costs or is it only the get(),used to retrieve the documentSnapshot itself, which should be calculated.所以,我的问题是第二个 get(),用于检索每个文档字段,是否计算在读取成本中,或者它只是用于检索 documentSnapshot 本身的 get(),它应该被计算。

Here is my full code:这是我的完整代码:

_setStoreOwnerObjByFetchingUserData(String userUid) async {

  Future<DocumentSnapshot<Map<String, dynamic>>> _userCollection =
  FirebaseFirestore.instance.collection('users').doc(userUid).get();

  await StoreOwner().updateOwnerData(
      userUid,
      await _userCollection.then((value) => value.get("shopName")),
      await _userCollection.then((value) => value.get("shopAddress")),
      await _userCollection.then((value) => value.get("shopType")),
      await _userCollection.then((value) => value.get("shopSize")),
      await _userCollection.then((value) => value.get("ownerName")),
      await _userCollection.then((value) => value.get("ownerNumber")),
      await _userCollection.then((value) => value.get("subscriptionStatus")));
}

Would the second get() ,used to retrieve each document field, be calculated in the read costs or is it only the get(),used to retrieve the documentSnapshot itself, which should be calculated.用于检索每个文档字段的第二个get()是在读取成本中计算的,还是仅用于检索 documentSnapshot 本身的 get() 应该计算。

You only pay for the " get() used to retrieve the DocumentSnapshot ".您只需为“用于检索DocumentSnapshotget() ”付费。 Once the asynchronous get() operation is complete the DocumentSnapshot is in memory in your app and you can call its get() method as many times as you want without any Firestore based cost .异步get()操作完成后, DocumentSnapshot在您的应用程序中位于 memory 中,您可以根据需要多次调用其get()方法,而无需任何基于 Firestore 的成本


THEREFORE it appears that in your code you query several times the database when it is not necessary.因此,在您的代码中,您似乎在不需要时多次查询数据库。 You should adapt it as follows:您应该按如下方式调整它:

_setStoreOwnerObjByFetchingUserData(String userUid) async {

  Future<DocumentSnapshot<Map<String, dynamic>>> _userCollection =
  FirebaseFirestore.instance.collection('users').doc(userUid).get();

  var docSnapshot = await _userCollection;

  await StoreOwner().updateOwnerData(
      userUid,
      docSnapshot.get("shopName"),
      docSnapshot.get("shopAddress"),
      //...
}

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

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