【问题标题】:How to implement a custom filter with mongodb schema?如何使用 mongodb 模式实现自定义过滤器?
【发布时间】:2021-09-27 20:44:55
【问题描述】:

我需要知道我可以实施任何方法来为复杂搜索设置自定义过滤器吗?

例如,

User.someMethodName((eachUser) => {
   const temp = eachUser.temperature;
   if(temp.mesure === 'C' && temp.value > 36) return true;
   if(temp.mesure === 'F' && convertToCel(temp.value) > 36) return true;
   return false;
})

类似的东西。我知道我可以等待 User.find({}) 并在其上使用数组过滤器方法进行过滤,但我担心在这种情况下,我将无法进行分页,而我可以使用猫鼬限制( )、skip() 方法。

任何帮助将不胜感激。谢谢!

编辑:刚刚发现可以使用 $redact 的聚合来实现这一点,但仍然没有弄清楚如何实现它来满足这个需求。

【问题讨论】:

    标签: mongodb filter pagination


    【解决方案1】:

    您可以使用 $switch 聚合框架运算符。

    https://docs.mongodb.com/manual/reference/operator/aggregation/switch/#example

    这里是根据您的要求的示例

    示例文档

    每个案例一个文档

    {
      "name" : "sensor1",
      "measure" : "C",
      "value" : 36 
    }
    
    {
      "name" : "sensor2",
      "measure" : "F",
      "value" : 78
    }
    
    {
      "name" : "sensor3",
      "measure" : "C",
      "value" : 12
    }
    
    {
      "name" : "sensor4",
      "measure" : "F",
      "value" : 113
    }
    

    查询

    使用不同的案例,您可以根据度量和值涵盖所有场景。


    • 华氏公式设置在 case 运算符中。
    • 如果需要,添加排序运算符和其他匹配条件。
    • $skip$limit 可以使用代码动态设置

    db.test.aggregate(
    [{$project : { 
     "name" : 1, 
     "measure": 1,
     "value": 1,
     "result" :
     { $switch:
        {
           branches: [
              { 
              //when your Celsius temp is greater and equals to 36
              case: { $and : [ { $eq : [ "$measure" , "C" ] },
                               { $gte : [ "$value" , 36 ] } ] },
              then: true    
              },
              { 
              //when your Celsius temp is less then 36
              case: { $and : [ { $eq : [ "$measure" , "C" ] },
                               { $lt : [ "$value" , 36 ] } ] },
              then: false
              },
              { 
              //when your Fahrenheit temp is greater and equals to 36
              case: { $and : [ { $eq : [ "$measure" , "F" ] },
                                { $gte : [ {"$multiply" : [ { "$sum": 
                                           ["$value", -32]}, 5/9]} , 36 ] } ] },
              },
              then: true    
              },
              { 
              //when your Fahrenheit temp is less than 36
              case: { $and : [ { $eq : [ "$measure" , "F" ] },
                               { $lt  : [ {"$multiply" : [ { "$sum": 
                                        ["$value", -32]}, 5/9]} , 36 ] } ] }, ] },
              then: false    
              }
           ],
           default: "error"
        }
    }}},
    {$skip : 0},
    {$limit : 2}])
    

    结果

    {
      "name" : "sensor1",
      "measure" : "C",
      "value" : 36,
      "result" : true
    }
    {
      "name" : "sensor2",
      "measure" : "F",
      "value" : 78,
      "result" : false
    }
    

    【讨论】:

    • 非常感谢,能够将其集成到我的解决方案中。完美运行!
    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2020-10-01
    • 1970-01-01
    • 2020-12-23
    • 2016-08-31
    • 1970-01-01
    相关资源
    最近更新 更多