Update Document
Definition
This document focuses on the update
method from a document object retrieved using ZkDatabase API. The update
method is used to modify the fields of an existing document with new values provided in the form of an object.
await document.update(updateFields: Record<string, any>);
Parameters
updateFields
:Record<string, any>
- An object representing the fields to be updated in the document. Each key corresponds to a field in the document, and the value is the new value intended for that field (e.g.,{ price: 20n }
).
Returns
The update
method returns a promise that resolves when the update operation is completed successfully. It raises an error if the document cannot be updated due to validation or other operational issues.
Example
Connect to the database, find a specific shirt document by name, and update its price:
import { CircuitString, UInt64 } from 'o1js';
import { Schema, ZkDatabase, Permission } from './index';
const zkdb = await ZkDatabase.connect({
userName: "chiro-user",
privateKey: "EKFTciRxyxshZjimay9sktsn7v5PvmC5zPq7q4JnitHUytxUVnFP",
environment: "node",
// This URL is for test environment
url: "https://api.zkdatabase.org/graphql",
});
await zkdb.auth.signIn();
class Shirt extends Schema.create({
name: CircuitString,
price: UInt64,
}) {}
type TShirt = typeof Shirt;
const collection = await zkdb
.db('zkdb_test')
.collection<TShirt>('test_collection');
const doc = await collection.findOne({ name: 'Test Shirt' });
if (doc) {
await doc.update({ price: 20n });
}
await zkdb.auth.signOut();