Performs a bulk update on multiple documents in the database.

This function sends an asynchronous request to the server to update multiple documents of the specified type in one operation. Each document should include its unique identifier under the name field, which will be used to match and update the existing records.

// Bulk update 'Employee' documents to change the department for multiple employees.
const employees: {name: string, department: string}[] = [
{name: 'EMP-001', department: 'HR'},
{name: 'EMP-002', department: 'Engineering'},
{name: 'EMP-003', department: 'Sales'},
];
const updatedEmployees = await bulkUpdate(employees);
console.log(updatedEmployees); // Logs the array of updated 'Employee' documents
// Bulk update 'Product' documents to change the price for multiple products.
const products: {name: string, price: number}[] = [
{name: 'PRD-123', price: 1099.99},
{name: 'PRD-456', price: 229.99},
];
const updatedProducts = await bulkUpdate(products);
console.log(updatedProducts); // Logs the array of updated 'Product' documents
// Bulk update 'Order' documents to modify the status for multiple orders.
const orders: {name: string, status: string}[] = [
{name: 'ORD-001', status: 'Shipped'},
{name: 'ORD-002', status: 'Delivered'},
];
const updatedOrders = await bulkUpdate(orders);
console.log(updatedOrders); // Logs the array of updated 'Order' documents

Throws an error if the request fails or if there is an issue with updating the documents.

  • Type Parameters

    Parameters

    • docs: T[]

      An array of documents to be updated. Each document must have a name field that serves as its unique identifier. The type T should extend an object with a name property and can include other fields that need to be updated.

    Returns Promise<T[]>

    A promise that resolves to an array of updated documents of type T. The returned documents include the updated data.