Search code examples
mongodbtypescriptloopback

How is MongoDB's "$inc" called in a Loopback 4 repository?


I'm attempting to do the following with Typescript and the loopback-connector-mongodb library, but get the error Object literal may only specify known properties.

interface Foo {
  likes: number;
}

// increase likes with MongoDB's '$inc'
async increaseLikes(id: string): Promise<void> {
  await this.fooRepository.updateById(id, {"$inc": {likes: 1}})
}

MongoDB docs


Solution

  • This is a Typescript compilation error even though the code is valid Javascript.

    The error can be suppressed with // @ts-ignore

    Docs

    interface Foo {
      likes: number;
    }
    
    // increase likes with MongoDB's '$inc'
    async increaseLikes(id: string): Promise<void> {
      // error suppressed to allow use of MongoDB's extended operators
      // @ts-ignore
      await this.fooRepository.updateById(id, {$inc: {likes: 1}})
    }