duande9301 2019-07-19 22:49
浏览 108
已采纳

在firestore中增加一个字段(php)

I am looking for a way to increment a field in firestore like in this documentation.

shard_ref.update("count", firebase.firestore.FieldValue.increment(1));

The only caveat is that I am trying to use firestore Client Sdk or the Admin Sdk for php.

What is a clean solution for incrementing a field in php. Does it require both read and write operations or is it possible with only write operation?

  • 写回答

1条回答 默认 最新

  • dongzhiqi0332 2019-07-20 00:29
    关注

    Firestore Client Sdk has the ability to increment a field. (use negative values to decrement). You can use it in the following way to update the field with $field_name:

    $data = [
                [
                       'path' => $field_name,
                       'value' => \Google\Cloud\Firestore\FieldValue::increment($increment_value);
                ]
            ];     
    $doc_ref->update($data);
    

    If you are using a batching you call it as follows

    $data[$field_name] = \Google\Cloud\Firestore\FieldValue::increment($increment_value);
    $batch->add_to_batch('update', $doc_ref, $data);
    

    Here is additional reference

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?