Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 同时更新不同路径上的两个字段是否算作云firestore中的两个写入?_Firebase_Flutter_Dart_Google Cloud Firestore - Fatal编程技术网

Firebase 同时更新不同路径上的两个字段是否算作云firestore中的两个写入?

Firebase 同时更新不同路径上的两个字段是否算作云firestore中的两个写入?,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,屏幕截图1: 在student>100>name,我想将'name'更新为'Bar' 屏幕截图2: 在student>100>考试>英语>分数,我想将分数更新为50 现在,我可以使用 var firestore = Firestore.instance; firestore.document('student/100').updateData({'name': 'Bar'}); firestore.document('student/100/exams/english').updateD

屏幕截图1:

student>100>name
,我想将
'name'
更新为
'Bar'


屏幕截图2:

student>100>考试>英语>分数
,我想将
分数
更新为
50


现在,我可以使用

var firestore = Firestore.instance;
firestore.document('student/100').updateData({'name': 'Bar'});
firestore.document('student/100/exams/english').updateData({'marks': 50});

问题:

此代码被调用两次:

var ref = firestore.document('student/100');
ref.snapshots().listen((event) {
  print('server updated'); // called 2 times
});
这算两次吗?如果是,如何将其更改为一次写入,因为两个字段属于同一路径
'student/100'

这算两次吗

是的,它将被计算为两次写入

当您使用Cloud Firestore时,您需要支付以下费用:

  • 执行的读取、写入和删除次数
  • 数据库使用的存储量,包括元数据和索引的开销
  • 您使用的网络带宽量
  • 如果是,如何将其更改为一次写入,因为这两个字段属于同一路径“student/100”

    在Firestore中,查询是浅层的,这意味着当您写入顶级集合下的文档时,除非指定指向该文档的路径,否则无法访问子集合中的文档。因此,不可能同时对顶级文档和子集合中的文档执行一次写入

    这算两次吗

    是的,它将被计算为两次写入

    当您使用Cloud Firestore时,您需要支付以下费用:

  • 执行的读取、写入和删除次数
  • 数据库使用的存储量,包括元数据和索引的开销
  • 您使用的网络带宽量
  • 如果是,如何将其更改为一次写入,因为这两个字段属于同一路径“student/100”


    在Firestore中,查询是浅层的,这意味着当您写入顶级集合下的文档时,除非指定指向该文档的路径,否则无法访问子集合中的文档。因此,不可能同时对顶级文档和子集合中的文档执行一次写入。

    @iKeepChangingName,如果我可以对Peter的优秀答案增加一个精度的话。实际上,从技术角度来看,
    学生
    集合和
    考试
    子集合之间根本没有任何关联:它们只是共享其路径的一部分,而没有其他内容。见@RenaudTarnec谢谢你的最后一句话,你也应该得到+1@iKeepChangingName请允许我对彼得出色的回答再加上一点精确性。实际上,从技术角度来看,
    学生
    集合和
    考试
    子集合之间根本没有任何关联:它们只是共享其路径的一部分,而没有其他内容。请参阅@RenaudTarnec谢谢最后一行,您也应该获得+1。由于数据竞争,我建议您使用事务,而不是更新数据。直接使用updateData有风险我建议您使用事务而不是updateData,因为数据竞争。直接使用updateData是有风险的