filterToBSON :: forall a. ( PersistField a)
             => Text
             -> Either a [a]
             -> PersistFilter
             -> DB.Field
filterToBSON fname v filt = case filt of
    Eq -> nullEq
    Ne -> nullNeq
    _  -> notEquality
  where
    dbv = toValue v
    notEquality = fname DB.=: [showFilter filt DB.:= dbv]

    nullEq = case dbv of
      DB.Null -> orDollar DB.=:
        [ [fname DB.:= DB.Null]
        , [fname DB.:= DB.Doc [existsDollar DB.:= DB.Bool False]]
        ]
      _ -> fname DB.:= dbv

    nullNeq = case dbv of
      DB.Null ->
        fname DB.:= DB.Doc
          [ showFilter Ne DB.:= DB.Null
          , existsDollar DB.:= DB.Bool True
          ]
      _ -> notEquality

    showFilter Ne = "$ne"
    showFilter Gt = "$gt"
    showFilter Lt = "$lt"
    showFilter Ge = "$gte"
    showFilter Le = "$lte"
    showFilter In = "$in"
    showFilter NotIn = "$nin"
    showFilter Eq = error "EQ filter not expected"
    showFilter (BackendSpecificFilter bsf) = throw $ PersistMongoDBError $ T.pack $ "did not expect BackendSpecificFilter " ++ T.unpack bsf
filterToBSON :: forall a. ( PersistField a)
             => Text
             -> Either a [a]
             -> PersistFilter
             -> DB.Field
filterToBSON fname v filt = case filt of
    Eq -> nullEq
    Ne -> nullNeq
    _  -> notEquality
  where
    dbv = toValue v
    notEquality = fname DB.=: [showFilter filt DB.:= dbv]

    nullEq = case dbv of
      DB.Null -> orDollar DB.=:
        [ [fname DB.:= DB.Null]
        , [fname DB.:= DB.Doc [existsDollar DB.:= DB.Bool False]]
        ]
      _ -> fname DB.:= dbv

    nullNeq = case dbv of
      DB.Null ->
        fname DB.:= DB.Doc
          [ showFilter Ne DB.:= DB.Null
          , existsDollar DB.:= DB.Bool True
          ]
      _ -> notEquality

    showFilter Ne = "$ne"
    showFilter Gt = "$gt"
    showFilter Lt = "$lt"
    showFilter Ge = "$gte"
    showFilter Le = "$lte"
    showFilter In = "$in"
    showFilter NotIn = "$nin"
    showFilter Eq = error "EQ filter not expected"
    showFilter (BackendSpecificFilter bsf) = throw $ PersistMongoDBError $ T.pack $ "did not expect BackendSpecificFilter " ++ T.unpack bsf
filterToBSON fname v filt = case filt of
    Eq -> nullEq
    Ne -> nullNeq
    _  -> notEquality
  where
    dbv = toValue v
    notEquality = fname DB.=: [showFilter filt DB.:= dbv]

    nullEq = case dbv of
      DB.Null -> orDollar DB.=:
        [ [fname DB.:= DB.Null]
        , [fname DB.:= DB.Doc [existsDollar DB.:= DB.Bool False]]
        ]
      _ -> fname DB.:= dbv

    nullNeq = case dbv of
      DB.Null ->
        fname DB.:= DB.Doc
          [ showFilter Ne DB.:= DB.Null
          , existsDollar DB.:= DB.Bool True
          ]
      _ -> notEquality

    showFilter Ne = "$ne"
    showFilter Gt = "$gt"
    showFilter Lt = "$lt"
    showFilter Ge = "$gte"
    showFilter Le = "$lte"
    showFilter In = "$in"
    showFilter NotIn = "$nin"
    showFilter Eq = error "EQ filter not expected"
    showFilter (BackendSpecificFilter bsf) = throw $ PersistMongoDBError $ T.pack $ "did not expect BackendSpecificFilter " ++ T.unpack bsf