Memperbarui dokumen Firestore dalam transaksi (asinkron)
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Memperbarui dokumen Firestore dalam transaksi (asinkron).
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code sample demonstrates how to update a Firestore document within an asynchronous transaction using Python.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eupdate_in_transaction\u003c/code\u003e function is defined to perform the document update, taking a transaction object and a document reference as input.\u003c/p\u003e\n"],["\u003cp\u003eThe code uses \u003ccode\u003e@firestore.async_transactional\u003c/code\u003e decorator to ensure the operation occurs as part of the transaction.\u003c/p\u003e\n"],["\u003cp\u003eTo authenticate, the code instructs you to set up Application Default Credentials, pointing to the "Set up authentication for a local development environment" documentation.\u003c/p\u003e\n"]]],[],null,["# Updating a Firestore document in a transaction (async).\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Transactions and batched writes](/firestore/native/docs/manage-data/transactions)\n- [Transactions and batched writes](https://firebase.google.com/docs/firestore/manage-data/transactions)\n\nCode sample\n-----------\n\n### Python\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n transaction = db.transaction()\n city_ref = db.collection(\"cities\").document(\"SF\")\n\n @firestore.async_transactional\n async def update_in_transaction(transaction, city_ref):\n snapshot = await city_ref.get(transaction=transaction)\n transaction.update(city_ref, {\"population\": snapshot.get(\"population\") + 1})\n\n await update_in_transaction(transaction, city_ref)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]