首页 > 解决方案 > 如果字段存在必须为真,但如果不存在则必须像真一样通过

问题描述

我想检查 pipeline aggregate( existeTransformacion) 中是否存在字段。如果此字段存在必须为真才能通过($match),如果为假,我需要从我的结果中排除,但如果不存在则必须通过。我怎样才能做到这一点?

 {
            //...more data
            "ubicacionActual": {
                "transformacion": {
                   "trabajando": true,
                }
            },
            //This field come from $project in this way 
            //$project: {existeTransformacion: '$ubicacionActual.transformacion.trabajando'}
            "existeTransformacion": true,
            "paso": 1
        },

所以基本上:

标签: mongodbmongooseaggregate

解决方案


您需要使用$or$exists运算符

{
  $match: {
    $or: [
      {
        "existeTransformacion": true
      },
      {
        "existeTransformacion": {
          $exists: false
        }
      }
    ]
  }
}

Mongo游乐场


推荐阅读