Skip to main content

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)).unwrap();

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 Result that can be checked with .isOk() / .isErr() or unwrapped with .unwrap().

Example

Connect to the database, find a specific document by name, and update its price:

import { ZkDatabase } from 'zkdb';

const zkdb = new ZkDatabase({
apiKey: 'zkdb_536aac02a1b7.c001b6b8f...da3aa4185d8d2ad07f0ae94aT',
// This URL is for test environment
url: "https://serverless.zkdatabase.org/graphql",
});
class Shirt extends Schema.create({
name: String,
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 });
}