Class QueryMapper
java.lang.Object
org.springframework.data.mongodb.core.convert.QueryMapper
- Direct Known Subclasses:
UpdateMapper
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, David Julia, Divya Srivastava, Gyungrai Wang, Ross Lawley
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classConverter to skip all properties after an association property was rendered.protected static classValue object to represent a field and its meta-information.static classExtension ofQueryMapper.Fieldto be backed with mapping metadata. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionQueryMapper(MongoConverter converter) Creates a newQueryMapperwith the givenMongoConverter. -
Method Summary
Modifier and TypeMethodDescriptionorg.bson.DocumentaddMetaAttributes(org.bson.Document source, @Nullable MongoPersistentEntity<?> entity) Adds missing$metarepresentation if required.protected @Nullable ObjectconvertAssociation(@Nullable Object source, @Nullable MongoPersistentProperty property) Converts the given source assuming it's actually an association to another object.protected @Nullable ObjectconvertAssociation(Object source, QueryMapper.Field field) @Nullable ObjectConverts the given raw id value into eitherObjectIdorString.@Nullable ObjectConverts the given raw id value into eitherObjectIdortargetType.protected @Nullable ObjectconvertSimpleOrDocument(Object source, @Nullable MongoPersistentEntity<?> entity) Retriggers mapping if the given source is aDocumentor simply invokes theprotected @Nullable ObjectconvertValueWithConversionContext(QueryMapper.Field documentField, @Nullable Object sourceValue, @Nullable Object value, PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter, MongoConversionContext conversionContext) createMapEntry(QueryMapper.Field field, @Nullable Object value) Creates a newMap.Entryfor the givenQueryMapper.Fieldwith the given value.protected QueryMapper.FieldcreatePropertyField(@Nullable MongoPersistentEntity<?> entity, String key, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) protected @Nullable ObjectdelegateConvertToMongoType(Object source, @Nullable MongoPersistentEntity<?> entity) Converts the given source Object to a mongo type with the type information of the original source type omitted.org.bson.DocumentgetMappedFields(org.bson.Document fieldsObject, @Nullable MongoPersistentEntity<?> entity) Maps fields to retrieve to theMongoPersistentEntitys properties.protected org.bson.DocumentgetMappedKeyword(QueryMapper.Field property, org.springframework.data.mongodb.core.convert.QueryMapper.Keyword keyword) Returns the mapped keyword considered defining a criteria for the given property.protected org.bson.DocumentgetMappedKeyword(org.springframework.data.mongodb.core.convert.QueryMapper.Keyword keyword, @Nullable MongoPersistentEntity<?> entity) Returns the givenDocumentrepresenting a keyword by mapping the keyword's value.org.bson.DocumentgetMappedObject(org.bson.conversions.Bson query, Optional<? extends MongoPersistentEntity<?>> entity) org.bson.DocumentgetMappedObject(org.bson.conversions.Bson query, @Nullable MongoPersistentEntity<?> entity) Replaces the property keys used in the givenDocumentwith the appropriate keys by using thePersistentEntitymetadata.getMappedObjectForField(QueryMapper.Field field, @Nullable Object rawValue) Extracts the mapped object value for given field out of rawValue taking nestedQueryMapper.Keywords into accountorg.bson.DocumentgetMappedSort(org.bson.Document sortObject, @Nullable MongoPersistentEntity<?> entity) Maps fields used for sorting to theMongoPersistentEntitys properties.protected @Nullable ObjectgetMappedValue(QueryMapper.Field documentField, @Nullable Object sourceValue) Returns the mapped value for the given source object assuming it's a value for the givenMongoPersistentProperty.MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> protected booleanisAssociationConversionNecessary(QueryMapper.Field documentField, @Nullable Object value) Returns whether the givenQueryMapper.Fieldrepresents an association reference that together with the given value requires conversion to aDBRefobject.protected final booleanisDBObject(@Nullable Object value) Checks whether the given value is aDBObject.protected final booleanisDocument(@Nullable Object value) Checks whether the given value is aDocument.protected booleanReturns whether the givenStringis a MongoDB keyword.protected booleanisNestedKeyword(@Nullable Object candidate) Returns whether the givenObjectis a keyword, i.e. if it's aDocumentwith a keyword key.protected booleanReturns whether the givenStringis the type key.
-
Field Details
-
LOGGER
protected static final org.apache.commons.logging.Log LOGGER
-
-
Constructor Details
-
QueryMapper
Creates a newQueryMapperwith the givenMongoConverter.- Parameters:
converter- must not be null.
-
-
Method Details
-
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 givenDocumentwith the appropriate keys by using thePersistentEntitymetadata.- 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 theMongoPersistentEntitys properties.
Also converts properties to their$metarepresentation 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 theMongoPersistentEntitys properties.
Also converts and potentially adds missing property$metarepresentation.- Parameters:
fieldsObject- must not be null.entity- can be null.- Returns:
- Since:
- 1.6
-
addMetaAttributes
public org.bson.Document addMetaAttributes(org.bson.Document source, @Nullable MongoPersistentEntity<?> entity) Adds missing$metarepresentation if required.- Parameters:
source- must not be null.entity- can be null.- Returns:
- never null.
- Since:
- 3.4
-
getMappedObjectForField
protected Map.Entry<String, @Nullable Object> getMappedObjectForField(QueryMapper.Field field, @Nullable Object rawValue) Extracts the mapped object value for given field out of rawValue taking nestedQueryMapper.Keywords into account- Parameters:
field-rawValue-- Returns:
-
createPropertyField
protected QueryMapper.Field createPropertyField(@Nullable MongoPersistentEntity<?> entity, String key, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) - Parameters:
entity-key-mappingContext-- Returns:
-
getMappedKeyword
protected org.bson.Document getMappedKeyword(org.springframework.data.mongodb.core.convert.QueryMapper.Keyword keyword, @Nullable MongoPersistentEntity<?> entity) Returns the givenDocumentrepresenting a keyword by mapping the keyword's value.- Parameters:
keyword- theDocumentrepresenting 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
protected @Nullable Object getMappedValue(QueryMapper.Field documentField, @Nullable Object sourceValue) Returns the mapped value for the given source object assuming it's a value for the givenMongoPersistentProperty.- Parameters:
documentField- the key the value will be bound to eventuallysourceValue- the source object to be mapped- Returns:
-
isAssociationConversionNecessary
protected boolean isAssociationConversionNecessary(QueryMapper.Field documentField, @Nullable Object value) Returns whether the givenQueryMapper.Fieldrepresents an association reference that together with the given value requires conversion to aDBRefobject. 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 theint0 as an indicator for an excluded field.- Parameters:
documentField- must not be null.value-- Returns:
-
convertSimpleOrDocument
protected @Nullable Object convertSimpleOrDocument(Object source, @Nullable MongoPersistentEntity<?> entity) Retriggers mapping if the given source is aDocumentor simply invokes the- Parameters:
source-entity-- Returns:
-
delegateConvertToMongoType
protected @Nullable 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
-
convertAssociation
-
convertAssociation
protected @Nullable Object convertAssociation(@Nullable Object source, @Nullable MongoPersistentProperty property) Converts the given source assuming it's actually an association to another object.- Parameters:
source-property-- Returns:
-
convertValueWithConversionContext
protected @Nullable Object convertValueWithConversionContext(QueryMapper.Field documentField, @Nullable Object sourceValue, @Nullable Object value, PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter, MongoConversionContext conversionContext) -
isDocument
Checks whether the given value is aDocument.- Parameters:
value- can be null.- Returns:
-
isDBObject
Checks whether the given value is aDBObject.- Parameters:
value- can be null.- Returns:
-
createMapEntry
protected final Map.Entry<String,Object> createMapEntry(QueryMapper.Field field, @Nullable Object value) Creates a newMap.Entryfor the givenQueryMapper.Fieldwith the given value.- Parameters:
field- must not be null.value- can be null.- Returns:
-
convertId
-
convertId
Converts the given raw id value into eitherObjectIdortargetType.- Parameters:
id- can be null.targetType-- Returns:
- the converted id or null if the source was already null.
- Since:
- 2.2
-
isNestedKeyword
-
isTypeKey
-
isKeyword
-
getMappingContext
public MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> getMappingContext() -
getConverter
-