Legna
Legna

Reputation: 468

If field exist must be true, but if not exist must be pass like true

I would like to check if a field exist in pipeline aggregate (existeTransformacion) . If this field exist must be true to pass ($match) and if is false I need to exclude from my results, but if not exist must be pass. How can I achieve this?

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

So basically:

Upvotes: 1

Views: 107

Answers (1)

Valijon
Valijon

Reputation: 13103

You need to use $or and $exists operators

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

MongoPlayground

Upvotes: 1

Related Questions