首页 > 解决方案 > 如何在 Loopback 4 中使用 MongoDB 扩展运算符?

问题描述

我正在使用loopback v4,我需要使用 MongoDB$unset扩展运算符。MongoDB 连接器上的文档间接声明它可以使用(见这里),但我找不到任何关于它应该如何在我的存储库上使用的示例/文档,你有什么提示吗?

标签: mongodbloopbackjsv4l2loopbackloopback4

解决方案


根据文档,您需要先修改设置DataSource

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
)


推荐阅读