[
  {
    "$project": {
      "cutoutScience": 0,
      "cutoutTemplate": 0,
      "cutoutDifference": 0
    }
  }, {
    "$lookup": {
      "from": "ZTF_alerts_aux",
      "localField": "objectId",
      "foreignField": "_id",
      "as": "aux"
    }
  }, {
    "$project": {
      "cross_matches": {
        "$arrayElemAt": [
          "$aux.cross_matches", 0
        ]
      },
      "prv_candidates": {
        "$filter": {
          "input": {
            "$arrayElemAt": [
              "$aux.prv_candidates", 0
            ]
          },
          "as": "item",
          "cond": {
            "$and": [
              {
                "$in": [
                  "$$item.programid", [
                    1, 2, 3
                  ]
                ]
              }, {
                "$lt": [
                  {
                    "$subtract": [
                      "$candidate.jd", "$$item.jd"
                    ]
                  }, 100
                ]
              }
            ]
          }
        }
      },
      "schemavsn": 1,
      "publisher": 1,
      "objectId": 1,
      "candid": 1,
      "candidate": 1,
      "classifications": 1,
      "coordinates": 1
    }
  }, {
    "$match": {
      "candidate.magpsf": {
        "$lt": 20
      },
      "candidate.isdiffpos": {
        "$in": [
          "1", "t"
        ]
      },
      "candidate.programid": {
        "$gt": 0
      },
      "candidate.ssdistnr": {
        "$lt": -1
      },
      "candidate.drb": {
        "$gt": 0.65
      }
    }
  }, {
    "$project": {
      "objectId": 1,
      "dist": "$candidate.distpsnr1",
      "sg": "$candidate.sgscore1",
      "rmag": "$candidate.srmag1",
      "gmag": "$candidate.sgmag1",
      "imag": "$candidate.simag1",
      "zmag": "$candidate.szmag1"
    }
  }, {
    "$project": {
      "_id": 0,
      "objectId": 1,
      "pointunderneath": {
        "$or": [
          {
            "$and": [
              {
                "$gt": [
                  "$dist", 0
                ]
              }, {
                "$lt": [
                  "$dist", 2
                ]
              }, {
                "$gt": [
                  "sg", 0.76
                ]
              }
            ]
          }, {
            "$and": [
              {
                "$and": [
                  {
                    "$gt": [
                      "$dist", 0
                    ]
                  }, {
                    "$lt": [
                      "$dist", 0.5
                    ]
                  }, {
                    "$eq": [
                      "sg", 0.5
                    ]
                  }
                ]
              }, {
                "$or": [
                  {
                    "$and": [
                      {
                        "$gt": [
                          "$rmag", 0
                        ]
                      }, {
                        "$lt": [
                          "$rmag", 17
                        ]
                      }
                    ]
                  }, {
                    "$and": [
                      {
                        "$gt": [
                          "$gmag", 0
                        ]
                      }, {
                        "$lt": [
                          "$gmag", 17
                        ]
                      }
                    ]
                  }, {
                    "$and": [
                      {
                        "$gt": [
                          "$imag", 0
                        ]
                      }, {
                        "$lt": [
                          "$imag", 17
                        ]
                      }
                    ]
                  }, {
                    "$and": [
                      {
                        "$gt": [
                          "$zmag", 0
                        ]
                      }, {
                        "$lt": [
                          "$zmag", 17
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }, {
            "$or": [
              {
                "$and": [
                  {
                    "$gt": [
                      "$rmag", 0
                    ]
                  }, {
                    "$lt": [
                      "$rmag", 12
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.49
                    ]
                  }, {
                    "$lt": [
                      "$dist", 20
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$rmag", 0
                    ]
                  }, {
                    "$lt": [
                      "$rmag", 15
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.49
                    ]
                  }, {
                    "$lt": [
                      "$dist", 5
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$rmag", 0
                    ]
                  }, {
                    "$lt": [
                      "$rmag", 15
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.8
                    ]
                  }, {
                    "$lt": [
                      "$dist", 20
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$gmag", 0
                    ]
                  }, {
                    "$lt": [
                      "$gmag", 12
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.49
                    ]
                  }, {
                    "$lt": [
                      "$dist", 20
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$gmag", 0
                    ]
                  }, {
                    "$lt": [
                      "$gmag", 12
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.8
                    ]
                  }, {
                    "$lt": [
                      "$dist", 20
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$imag", 0
                    ]
                  }, {
                    "$lt": [
                      "$imag", 12
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.49
                    ]
                  }, {
                    "$lt": [
                      "$dist", 20
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$imag", 0
                    ]
                  }, {
                    "$lt": [
                      "$imag", 14.5
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.49
                    ]
                  }, {
                    "$lt": [
                      "$dist", 5
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$imag", 0
                    ]
                  }, {
                    "$lt": [
                      "$imag", 15
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.8
                    ]
                  }, {
                    "$lt": [
                      "$dist", 20
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$zmag", 0
                    ]
                  }, {
                    "$lt": [
                      "$zmag", 11.5
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.49
                    ]
                  }, {
                    "$lt": [
                      "$dist", 10
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$zmag", 0
                    ]
                  }, {
                    "$lt": [
                      "$zmag", 14
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.49
                    ]
                  }, {
                    "$lt": [
                      "$dist", 2.5
                    ]
                  }
                ]
              }, {
                "$and": [
                  {
                    "$gt": [
                      "$zmag", 0
                    ]
                  }, {
                    "$lt": [
                      "$zmag", 15
                    ]
                  }, {
                    "$gt": [
                      "sg", 0.8
                    ]
                  }, {
                    "$lt": [
                      "$dist", 20
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  }, {
    "$match": {
      "pointunderneath": false
    }
  }, {
    "$project": {
      "candid": 1,
      "objectId": 1,
      "annotations.comment": "fast!"
    }
  }
]
