【问题标题】:multiple search and filter data returning count of each filter in nestjs mongodb多重搜索和过滤数据返回nestjs mongodb中每个过滤器的计数
【发布时间】:2022-10-25 06:43:05
【问题描述】:

我想按字段过滤数据库中的项目,并返回每个过滤器的计数。 Example is when I select to return specific brand names I want to see the number of brand name available.

有没有我可以使用的搜索服务,比如 Azure 搜索,或者在 NestJs 和 mongodb 中实现它的方法

这是数据库集合

{
  "brand": "Screaming Eagle, The Flight",
  "producer": "Screaming Eagle",
  "productionCountry": "America",
  "region": "Napa Valley",
  "appellation": "Oakville",
  "vintage": "2016",
  "grape": "Cabernet Sauvignon",
  "maturity": "25",
  "case": "3 bottles",
  "origin": "SECONDARYMARKET",
  "type": "Red"
},{
  "brand": "Joseph Phelps, Insignia",
  "producer": "Joseph Phelps",
  "productionCountry": "America",
  "region": "Napa Valley",
  "appellation": "St. Helena",
  "vintage": "2012",
  "grape": "Cabernet Sauvignon",
  "maturity": "25",
  "case": "6 bottles",
  "origin": "SECONDARYMARKET",
  "type": "Red"
},{
  "brand": "Joseph Phelps, Insignia",
  "producer": "Joseph Phelps",
  "productionCountry": "America",
  "region": "Napa Valley",
  "appellation": "St. Helena",
  "vintage": "2012",
  "grape": "Cabernet Sauvignon",
  "maturity": "25",
  "case": "6 bottles",
  "origin": "SECONDARYMARKET",
  "type": "Red"
},{
  "brand": "Continuum",
  "producer": "Continuum",
  "productionCountry": "America",
  "region": "Napa Valley",
  "appellation": "Oakville",
  "vintage": "2017",
  "grape": "Cabernet Sauvignon",
  "maturity": "26",
  "case": "6 bottles",
  "origin": "SECONDARYMARKET",
  "type": "Red"
},{
  "brand": "Continuum",
  "producer": "Continuum",
  "productionCountry": "America",
  "region": "Napa Valley",
  "appellation": "Oakville",
  "vintage": "2017",
  "grape": "Cabernet Sauvignon",
  "maturity": "26",
  "case": "6 bottles",
  "origin": "SECONDARYMARKET",
  "type": "Red"
}

【问题讨论】:

    标签: mongodb elasticsearch search nestjs azure-cognitive-search


    【解决方案1】:

    这称为“方面”,它受 Azure 认知搜索和 MongoDB 的支持。在 Azure 认知搜索中,您必须将要获取计数的字段的 facetable 设置为 true,例如 brand,然后在查询中指定 "facets": ["brand"]

    有关 Azure 搜索的详细信息,请参阅:https://learn.microsoft.com/en-us/azure/search/search-faceted-navigation

    这可以在 MongoDb 中使用 $facet 聚合运算符,您可以在此处阅读更多信息:https://www.mongodb.com/docs/manual/reference/operator/aggregation/facet/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多