0

I'm working with loopback v4 and I need to use the MongoDB $unset extended operator. Documentation on MongoDB connector indirectly states that it can be used (see here) , but I can't find any example/documentation on how is it supposed to be used on my repository, do you have any hints?

sertal70
  • 493
  • 3
  • 10

1 Answers1

2

According to the documentation, you need to modify the settings in DataSource first.

xxx.datasource.ts

export class XxxDataSource extends juggler.DataSource {
    static dataSourceName = '...';

    constructor() {
        super({
            "name": "...",
            "connector": "mongodb",
            "url": "...",
            "database": "...",
            "allowExtendedOperators": true // <= !!!! default is false
        });
    }
}

xxx.controller.ts

return await this.xxxRepository.updateById(
    "....id....",
    {
        $unset: {
            test: ""
        }
    } as any // <= !!!! you can using `$unset` now, add `as any` to avoid type error
)

Zhikai Xiong
  • 357
  • 2
  • 9
  • Thank you for your answer @Zhikai Xiong! Actually I already enabled extended operator in my datasource connection, but I was stuck on `updateById()` because I missed the `as any` to allow ts compilation! – sertal70 Sep 11 '19 at 08:38