@Override
    public SamlIdPMetadataDocument fetchInternal(final Optional<SamlRegisteredService> registeredService) {
        if (registeredService.isPresent()) {
            val query = new Query();
            val appliesTo = getAppliesToFor(registeredService);
            query.addCriteria(Criteria.where("appliesTo").is(appliesTo));
            LOGGER.trace("Fetching SAML IdP metadata document for [{}] from [{}]", appliesTo, this.collectionName);
            val document = mongoTemplate.findOne(query, SamlIdPMetadataDocument.class, this.collectionName);
            if (document != null && document.isValid()) {
                LOGGER.trace("Found SAML IdP metadata document [{}] for [{}] from [{}]", document, appliesTo, this.collectionName);
                return document;
            }
        }
        LOGGER.trace("Fetching SAML IdP metadata document from [{}]", this.collectionName);
        return mongoTemplate.findOne(new Query(), SamlIdPMetadataDocument.class, this.collectionName);
    }
This is {@link MongoDbSamlIdPMetadataLocator}.

@author Misagh Moayyed
@since 6.0.0
support/cas-server-support-saml-idp-metadata-mongo/src/main/java/org/apereo/cas/support/saml/idp/metadata/MongoDbSamlIdPMetadataLocator.java