Function updateById

  • Update a document by id

    Throws

    mongoose error

    Example

    import { updateById } from '@mdazad/common-utils';
    import { User } from '../models/user';

    const updatedUser = await updateById('id', User, { name: 'John Doe' });

    Example

    if you want to populate fields in response

    import { updateById } from '@mdazad/common-utils';
    import { User } from '../models/user';

    const updatedUser = await updateById('id', User, { name: 'John Doe' }, ['posts']);

    Parameters

    • id: string

      id of the document

    • model: Model<Document<any, any, any>, {}, {}, {}, any>

      mongoose model

    • data: any

      data to update

    • populateFields: string[] = []

      fields to populate

    Returns Promise<undefined | null | Document<any, any, any> & {
        _id: ObjectId;
    }>

    updated document

Generated using TypeDoc