org.springframework.data.mongodb.core.convert

Class QueryMapper

  • Direct Known Subclasses:
    UpdateMapper


    public class QueryMapper
    extends Object
    A helper class to encapsulate any modifications of a Query object before it gets submitted to the database.
    Author:
    Jon Brisbin, Oliver Gierke, Patryk Wasik, Thomas Darimont, Christoph Strobl, Mark Paluch
    • Method Detail

      • getMappedObject

        public org.bson.Document getMappedObject(org.bson.conversions.Bson query,
                                                 Optional<? extends MongoPersistentEntity<?>> entity)
      • getMappedObject

        public org.bson.Document getMappedObject(org.bson.conversions.Bson query,
                                                 @Nullable
                                                 MongoPersistentEntity<?> entity)
        Replaces the property keys used in the given Document with the appropriate keys by using the PersistentEntity metadata.
        Parameters:
        query - must not be null.
        entity - can be null.
        Returns:
      • getMappedSort

        public org.bson.Document getMappedSort(org.bson.Document sortObject,
                                               @Nullable
                                               MongoPersistentEntity<?> entity)
        Maps fields used for sorting to the MongoPersistentEntitys properties.
        Also converts properties to their $meta representation if present.
        Parameters:
        sortObject -
        entity -
        Returns:
        Since:
        1.6
      • getMappedFields

        public org.bson.Document getMappedFields(org.bson.Document fieldsObject,
                                                 @Nullable
                                                 MongoPersistentEntity<?> entity)
        Maps fields to retrieve to the MongoPersistentEntitys properties.
        Also converts and potentially adds missing property $meta representation.
        Parameters:
        fieldsObject - must not be null.
        entity - can be .
        Returns:
        Since:
        1.6
      • getMappedObjectForField

        protected Map.Entry<String,Object> getMappedObjectForField(QueryMapper.Field field,
                                                                   Object rawValue)
        Extracts the mapped object value for given field out of rawValue taking nested Keywords into account
        Parameters:
        field -
        rawValue -
        Returns:
      • getMappedKeyword

        protected org.bson.Document getMappedKeyword(org.springframework.data.mongodb.core.convert.QueryMapper.Keyword keyword,
                                                     @Nullable
                                                     MongoPersistentEntity<?> entity)
        Returns the given Document representing a keyword by mapping the keyword's value.
        Parameters:
        keyword - the Document representing a keyword (e.g. $ne : … )
        entity -
        Returns:
      • getMappedKeyword

        protected org.bson.Document getMappedKeyword(QueryMapper.Field property,
                                                     org.springframework.data.mongodb.core.convert.QueryMapper.Keyword keyword)
        Returns the mapped keyword considered defining a criteria for the given property.
        Parameters:
        property -
        keyword -
        Returns:
      • getMappedValue

        @Nullable
        protected Object getMappedValue(QueryMapper.Field documentField,
                                                  Object value)
        Returns the mapped value for the given source object assuming it's a value for the given MongoPersistentProperty.
        Parameters:
        value - the source object to be mapped
        property - the property the value is a value for
        newKey - the key the value will be bound to eventually
        Returns:
      • isAssociationConversionNecessary

        protected boolean isAssociationConversionNecessary(QueryMapper.Field documentField,
                                                           @Nullable
                                                           Object value)
        Returns whether the given QueryMapper.Field represents an association reference that together with the given value requires conversion to a DBRef object. We check whether the type of the given value is compatible with the type of the given document field in order to deal with potential query field exclusions, since MongoDB uses the int 0 as an indicator for an excluded field.
        Parameters:
        documentField - must not be null.
        value -
        Returns:
      • delegateConvertToMongoType

        @Nullable
        protected Object delegateConvertToMongoType(Object source,
                                                              @Nullable
                                                              MongoPersistentEntity<?> entity)
        Converts the given source Object to a mongo type with the type information of the original source type omitted. Subclasses may overwrite this method to retain the type information of the source type on the resulting mongo type.
        Parameters:
        source -
        entity -
        Returns:
        the converted mongo type or null if source is null
      • isDocument

        protected final boolean isDocument(@Nullable
                                           Object value)
        Checks whether the given value is a Document.
        Parameters:
        value - can be null.
        Returns:
      • isDBObject

        protected final boolean isDBObject(@Nullable
                                           Object value)
        Checks whether the given value is a DBObject.
        Parameters:
        value - can be null.
        Returns:
      • isNestedKeyword

        protected boolean isNestedKeyword(Object candidate)
        Returns whether the given Object is a keyword, i.e. if it's a Document with a keyword key.
        Parameters:
        candidate -
        Returns:
      • isKeyword

        protected boolean isKeyword(String candidate)
        Returns whether the given String is a MongoDB keyword. The default implementation will check against the set of registered keywords returned by #getKeywords().
        Parameters:
        candidate -
        Returns: