Update Document
Update an existing document with new field values.
Definition
await zkdb.db(databaseName: string)
.collection(collectionName: string)
.findOne(filter: Record<string, any>)
.update(updateFields: Record<string, any>): Promise<boolean>;
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
Promise<boolean>
: Returnstrue
when the document is successfully updated with the new field values. The updated document is immediately available for queries and the changes will be included in the next zero-knowledge proof generation cycle for blockchain verification.
Example
import { ZkDatabase } from 'zkdb';
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();
const dbTest = zkdb.db('zkdb_test');
const collection = dbTest.collection('Shirt');
const doc = await collection.findOne({ name: 'Orochi' });
const result = await doc.update({ price: 25n * 10n });
console.log('Update result:', result);
await zkdb.auth.signOut();
Result:
Update result: true