def filters_to_mql(
    filters: MetadataFilters, metadata_key: str = "metadata"
) -> Dict[str, Any]:
    """
    Converts Llama-index's MetadataFilters into the MQL expected by $vectorSearch query.

    We are looking for something like

    "filter": {
            "$and": [
                { "metadata.genres": { "$eq": "Comedy" } },
                { "metadata.year": { "$gt": 2010 } }
            ]
    },

    See: See https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/#atlas-vector-search-pre-filter

    Args:
        filters: MetadataFilters object
        metadata_key: The key under which metadata is stored in the document

    Returns:
        MQL version of the filter.

    """
    if filters is None:
        return {}

    def prepare_key(key: str) -> str:
        return (
            f"{metadata_key}.{key}" if not key.startswith(f"{metadata_key}.") else key
        )

    if len(filters.filters) == 1:
        mf = filters.filters[0]
        mql = {
            prepare_key(mf.key): {map_lc_mql_filter_operators(mf.operator): mf.value}
        }
    elif filters.condition == FilterCondition.AND:
        mql = {
            "$and": [
                {
                    prepare_key(mf.key): {
                        map_lc_mql_filter_operators(mf.operator): mf.value
                    }
                }
                for mf in filters.filters
            ]
        }
    elif filters.condition == FilterCondition.OR:
        mql = {
            "$or": [
                {
                    prepare_key(mf.key): {
                        map_lc_mql_filter_operators(mf.operator): mf.value
                    }
                }
                for mf in filters.filters
            ]
        }
    else:
        logger.debug(f"filters.condition not recognized. Returning empty dict")
        mql = {}

    return mql

    Converts Llama-index's MetadataFilters into the MQL expected by $vectorSearch query.

    We are looking for something like

    "filter": {
            "$and": [
                { "metadata.genres": { "$eq": "Comedy" } },
                { "metadata.year": { "$gt": 2010 } }
            ]
    },

    See: See https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/#atlas-vector-search-pre-filter

    Args:
        filters: MetadataFilters object
        metadata_key: The key under which metadata is stored in the document

    Returns:
        MQL version of the filter.

    
llama-index-integrations/vector_stores/llama-index-vector-stores-mongodb/llama_index/vector_stores/mongodb/pipelines.py