mongodb 如何优化过滤功能

ifmq2ha2  于 2022-11-28  发布在  Go
关注(0)|答案(1)|浏览(117)

我正在研究angular,nestjs,graphql和mongodb。所以我有一个模态组件来过滤包含多个字段的数据。我写了这段代码,但我确信这不是一个好的实践,还有其他方法来实现同样的目标。为了学习如何写干净的代码,我希望其中一个英雄能帮助我
我试过了,但我觉得不太好
`

let matchingValue: any;
    let matchingValuePipeline: any;
    // const { bySn, byBank, byClient, byModel, byRegion, bySite, byTerminalId } =
    //   search;
    for (let item in search) {
      if (search[item] === '') {
        matchingValue = { inUse: true };
        matchingValuePipeline = { bindedSuperAdmin: name };
      }
    }
    //-----------------------------
    if (search.byBank) {
      matchingValuePipeline = { bindedBanque: search.byBank };
    }
    if (search.byBank && search.byClient) {
      matchingValuePipeline = {
        bindedBanque: search.byBank,
        bindedClient: search.byClient,
      };
    }
    if (search.byBank && search.byClient && search.bySite) {
      matchingValuePipeline = {
        bindedBanque: search.byBank,
        bindedClient: search.byClient,
        nameUser: search.bySite,
      };
    }
    if (search.byBank && search.byClient && search.bySite && search.byModel) {
      matchingValuePipeline = {
        bindedBanque: search.byBank,
        bindedClient: search.byClient,
        nameUser: search.bySite,
      };
      matchingValue = { inUse: true, model: search.byModel };
    }
    if (
      search.byBank &&
      search.byClient &&
      search.bySite &&
      search.byModel &&
      search.byRegion
    ) {
      matchingValuePipeline = {
        bindedBanque: search.byBank,
        bindedClient: search.byClient,
        nameUser: search.bySite,
        region: search.byRegion,
      };
      matchingValue = { inUse: true, model: search.byModel };
    }
    if (
      search.byBank &&
      search.byClient &&
      search.bySite &&
      search.byModel &&
      search.byRegion &&
      search.bySn
    ) {
      matchingValuePipeline = {
        bindedBanque: search.byBank,
        bindedClient: search.byClient,
        nameUser: search.bySite,
        region: search.byRegion,
      };
      matchingValue = { inUse: true, model: search.byModel, sn: search.bySn };
    }
    if (
      search.byBank &&
      search.byClient &&
      search.bySite &&
      search.byModel &&
      search.byRegion &&
      search.bySn &&
      search.byTerminalId
    ) {
      matchingValuePipeline = {
        bindedBanque: search.byBank,
        bindedClient: search.byClient,
        nameUser: search.bySite,
        region: search.byRegion,
      };
      matchingValue = {
        inUse: true,
        model: search.byModel,
        sn: search.bySn,
        terminalId: search.byTerminalId,
      };
    }

    if (search.byClient) {
      matchingValuePipeline = { bindedClient: search.byClient };
    }
    if (search.bySite) {
      matchingValuePipeline = { nameUser: search.bySite };
    }
    if (search.byRegion) {
      matchingValuePipeline = { region: search.byRegion };
    }
    if (search.bySn) {
      matchingValue = { sn: search.bySn };
    }
    if (search.byModel) {
      matchingValue = { model: search.byModel };
    }
    if (search.byTerminalId) {
      matchingValue = { terminalId: search.byTerminalId };
    }

    //--------------------------------------
    // for testing purpose
    console.log('matchingValue', matchingValue);
    console.log('matchingValuePipeline', matchingValuePipeline);
    console.log('search', search);

    let filter;
    if (role === ROLEV1.MS_TECH_SOFT) {
      filter = await this.modelTpe.aggregate([
        {
          $match: matchingValue,
        },
        {
          $lookup: {
            from: 'profiles',
            localField: 'merchantName',
            foreignField: 'nameUser',
            as: 'listTpe',
            pipeline: [
              {
                $match: matchingValuePipeline,
              },
            ],
          },
        },

`

search定义如下,作为DTO

@InputType()
export class SearchMethods {
  @Field({ nullable: true })
  bindedBanque?: string;
  @Field({ nullable: true })
  bindedClient?: string;
  @Field({ nullable: true })
  nameUser?: string;
  @Field({ nullable: true })
  region?: string;
  @Field({ nullable: true })
  model?: string;
  @Field({ nullable: true })
  sn: string;
  @Field({ nullable: true })
  terminalId?: string;
}

要达到所有条件我至少会写100行

dl5txlt9

dl5txlt91#

您可以向对象添加属性,例如

let matchingValue = {};
if (search.byBank) matchingValue.bindedBanque = search.byBank;
if (search.byClient) matchingValue.bindedClient = search.byClient;
if (search.bySite) matchingValue.nameUser = search.bySite;
matchingValue.nameUser = {$in: [a, b]}

modelTpe.aggregate([
   { $match: matchingValue }
])

您可以写入matchingValuePipeline["bindedBanque"]来代替matchingValuePipeline.bindedBanque,其中"bindedBanque"也可以是变数。
您也可以移除密钥:

delete matchingValue.bindedBanque;

不需要为每个组合配备专用的 shell 。

相关问题