Decode a BSON document.

def _elements_to_dict(data, position, obj_end, opts):
    """Decode a BSON document."""
    result = opts.document_class()
    pos = position
    for key, value, pos in _iterate_elements(data, position, obj_end, opts):
        result[key] = value
    if pos != obj_end:
        raise InvalidBSON('bad object or element length')
    return result