如果值为null,则如何在mongodb查询中忽略一个参数

发布于 2025-01-31 05:37:11 字数 2744 浏览 1 评论 0原文

我想根据函数的参数传递的多个参数,从MongoDB中的产品集合中获取文档。

我正在将Nest JS服务器与Mongoose一起连接到MongoDB数据库。

在这里,在此功能中,我正在尝试获取产品 1. ColorCode等于传递的条件代码作为参数(只有当颜色代码传递为参数不是null的情况下,如果ColorCode参数为null-查询在获取数据时不应考虑colorcode,则应将所有文档获取所有文档都有colorcode到任何东西)) 2. categoryCode等于传递的类别代码作为参数(与上述相同的条件)。

我的功能是:

async getSearchedProducts(
    title: string,
    category: string,
    brand: string,
    color: string,
    productCode: string,
    sellerCode: string,
    pageNumber: string,
  ) {
    const products = await this.productModel.find({
      colorCode:
        color === undefined || 'null' ? { $ne: null } : color,
      categoryCode: category === undefined || 'null' ? { $ne: null } : category,
      brandCode: brand === undefined || 'null' ? { $ne: null } : brand,
      productCode:
        productCode === undefined || 'null' ? { $ne: null } : productCode,
      // brandCode : brand
    });
    return products;
  }

所需的结果示例:

假设产品收集是:

[
{
"title" : "iphone x",
"colorCode":"black",
"categoryCode":"mobile"
},
{
"title":"MacBook Pro",
"colorCode":"black",
"categoryCode":"laptop"
},
{
"title":"MacBook Air",
"colorCode":"black",
"categoryCode":"laptop"
}
]

让我们假设功能如下:

async getSearchedProducts(
        title: string //is null,
        category: string. //is null,
        color: string, //black
      ) {
        const products = await this.productModel.find({
          colorCode:
            color === undefined || 'null' ? { $ne: null } : color,
          categoryCode: category === undefined || 'null' ? { $ne: null } : category,
        });
        return products;
      }

这里所需的结果是:

[
    {
    "title" : "iphone x",
    "colorCode":"black",
    "categoryCode":"mobile"
    },
    {
    "title":"MacBook Pro",
    "colorCode":"black",
    "categoryCode":"laptop"
    },
    {
    "title":"MacBook Air",
    "colorCode":"black",
    "categoryCode":"laptop"
    }
    ] - all three documents

示例2:

async getSearchedProducts(
        title: string //is null,
        category: string. //laptop,
        color: string, //black
      ) {
        const products = await this.productModel.find({
          colorCode:
            color === undefined || 'null' ? { $ne: null } : color,
          categoryCode: category === undefined || 'null' ? { $ne: null } : category,
        });
        return products;
      }

这里所需的结果是:

[
    {
    "title":"MacBook Pro",
    "colorCode":"black",
    "categoryCode":"laptop"
    },
    {
    "title":"MacBook Air",
    "colorCode":"black",
    "categoryCode":"laptop"
    }
    ] - 2 documents

任何帮助都非常感谢。 谢谢。

i would like to fetch document from a products collection in mongoDB based on multiple paramters passed as arguments to the function.

I am using nest js server with mongoose to connect to mongoDB database.

here, in this function i am trying to fetch products where
1.colorCode is equal to conditionCode passed as argument (only if the colorCode passed as argument is not null, if colorCode argument is null - the query should not consider colorCode while fetching data, it should fetch all the documents having colorCode to anything)
2.categoryCode is equal to categoryCode passed as argument (same condition as above).

My function is :

async getSearchedProducts(
    title: string,
    category: string,
    brand: string,
    color: string,
    productCode: string,
    sellerCode: string,
    pageNumber: string,
  ) {
    const products = await this.productModel.find({
      colorCode:
        color === undefined || 'null' ? { $ne: null } : color,
      categoryCode: category === undefined || 'null' ? { $ne: null } : category,
      brandCode: brand === undefined || 'null' ? { $ne: null } : brand,
      productCode:
        productCode === undefined || 'null' ? { $ne: null } : productCode,
      // brandCode : brand
    });
    return products;
  }

desired outcome example :

lets say the products collection is :

[
{
"title" : "iphone x",
"colorCode":"black",
"categoryCode":"mobile"
},
{
"title":"MacBook Pro",
"colorCode":"black",
"categoryCode":"laptop"
},
{
"title":"MacBook Air",
"colorCode":"black",
"categoryCode":"laptop"
}
]

let assume the function as follow :

async getSearchedProducts(
        title: string //is null,
        category: string. //is null,
        color: string, //black
      ) {
        const products = await this.productModel.find({
          colorCode:
            color === undefined || 'null' ? { $ne: null } : color,
          categoryCode: category === undefined || 'null' ? { $ne: null } : category,
        });
        return products;
      }

here desired outcome is :

[
    {
    "title" : "iphone x",
    "colorCode":"black",
    "categoryCode":"mobile"
    },
    {
    "title":"MacBook Pro",
    "colorCode":"black",
    "categoryCode":"laptop"
    },
    {
    "title":"MacBook Air",
    "colorCode":"black",
    "categoryCode":"laptop"
    }
    ] - all three documents

example 2 :

async getSearchedProducts(
        title: string //is null,
        category: string. //laptop,
        color: string, //black
      ) {
        const products = await this.productModel.find({
          colorCode:
            color === undefined || 'null' ? { $ne: null } : color,
          categoryCode: category === undefined || 'null' ? { $ne: null } : category,
        });
        return products;
      }

here desired outcome is :

[
    {
    "title":"MacBook Pro",
    "colorCode":"black",
    "categoryCode":"laptop"
    },
    {
    "title":"MacBook Air",
    "colorCode":"black",
    "categoryCode":"laptop"
    }
    ] - 2 documents

Any help is really appreciated.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2025-02-07 05:37:11

我认为三元操作员存在问题,并将其修复如下:

async getSearchedProducts(
        title: string //is null,
        category: string. //laptop,
        color: string, //black
      ) {
        const products = await this.productModel.find({
          colorCode:
           typeof color === 'undefined' || color === null ? { $ne: null } : color,
          categoryCode: typeof category === 'undefined' || category === null ? { $ne: null } : category,
        });
        return products;
      }

I think there is a problem in ternary operator and fixed it as follows:

async getSearchedProducts(
        title: string //is null,
        category: string. //laptop,
        color: string, //black
      ) {
        const products = await this.productModel.find({
          colorCode:
           typeof color === 'undefined' || color === null ? { $ne: null } : color,
          categoryCode: typeof category === 'undefined' || category === null ? { $ne: null } : category,
        });
        return products;
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文