<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _exists(field, value, document):
 """ Ensures a document has a given field or not. ``value`` must be either True or False, otherwise a MalformedQueryException is raised """

    if value not in (True, False):
        raise MalformedQueryException("'$exists' must be supplied a boolean")

    if value:
        return field in document
    else:
        return field not in document