mongodbloopbackjsv4l2loopbackloopback4

How to use MongoDB extended operators in Loopback 4?


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?


Solution

  • 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
    )