def __eq__(a, b):
    """Check equality of two instances."""
    if not (isinstance(a, b.__class__) or isinstance(b, a.__class__)):
        return False
    if a._meta.database != b._meta.database:
        return False
    if a._meta.collection != b._meta.collection:
        return False
    a_id = a._document.get('_id', None)
    b_id = b._document.get('_id', None)
    if not (a_id and b_id):
        return False
    return a_id == b_id
