Class BsonUtils
java.lang.Object
org.springframework.data.mongodb.util.BsonUtils
Internal API for operations on
Bson elements that can be either Document or DBObject.- Since:
- 2.0
- Author:
- Christoph Strobl, Mark Paluch
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.bson.DocumentThe empty document (immutable). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddAllToMap(org.bson.conversions.Bson target, Map<String, ?> source) Add all entries from the given sourceMapto the target.static voidstatic org.bson.conversions.BsonReturns the given source object asBson, i.e.static Collection<?> asCollection(Object source) Returns given object asCollection.static org.bson.DocumentasDocument(@Nullable org.bson.conversions.Bson bson) Return theBsonobject asDocument.static org.bson.DocumentasDocument(@Nullable org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry) Return theBsonobject asDocument.asMap(@Nullable org.bson.conversions.Bson bson) Return theBsonobject asMap.asMap(@Nullable org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry) Return theBsonobject asMap.static org.bson.DocumentasMutableDocument(org.bson.conversions.Bson bson) Return theBsonobject as mutableDocumentcontaining all entries fromBson.static booleanCheck if a given entry (key/value pair) is present in the givenBson.static <T> @Nullable Tstatic booleanReturns whether the underlyingbsonhas a value (null or non-null) for the givenkey.static booleanstatic booleanisJsonArray(@Nullable String value) Check if a given String looks likeparsablejson array.static booleanisJsonDocument(@Nullable String value) Check if a given String looks likeparsablejson.static org.bson.DocumentmapEntries(org.bson.Document source, Function<Map.Entry<String, Object>, String> keyMapper, Function<Map.Entry<String, Object>, Object> valueMapper) static org.bson.DocumentmapValues(org.bson.Document source, BiFunction<String, Object, Object> valueMapper) static org.bson.Documentmerge(org.bson.Document... documents) Merge the givendocumentsinto on in the given order.static org.bson.Documentparse(String json, @Nullable CodecRegistryProvider codecRegistryProvider) Parse the given json toDocumentapplying transformations as specified by a potentially givenCodec.static booleanremoveNullId(org.bson.conversions.Bson bson) Remove_id : nullfrom the givenBsonif present.static @Nullable ObjectresolveValue(Map<String, Object> source, String key) Resolve the value for a given key.static @Nullable ObjectresolveValue(Map<String, Object> source, FieldName fieldName) Resolve the value for a givenfield name.static @Nullable ObjectresolveValue(org.bson.conversions.Bson bson, String key) Resolve the value for a given key.static @Nullable ObjectresolveValue(org.bson.conversions.Bson bson, FieldName fieldName) Resolve the value for a givenfield name.static org.bson.BsonValuesimpleToBsonValue(@Nullable Object source) Convert a given simple value (eg.static org.bson.BsonValuesimpleToBsonValue(@Nullable Object source, org.bson.codecs.configuration.CodecRegistry codecRegistry) Convert a given simple value (eg.static booleansupportsBson(Object source) Returns the given source can be used/converted asBson.static org.bson.DocumenttoDocumentOrElse(String source, Function<String, org.bson.Document> orElse) static ObjecttoJavaType(org.bson.BsonValue value) Extract the corresponding plain value fromBsonValue.static @Nullable StringtoJson(@Nullable org.bson.Document source) Serialize the givenDocumentas Json applying default codecs if necessary.
-
Field Details
-
EMPTY_DOCUMENT
public static final org.bson.Document EMPTY_DOCUMENTThe empty document (immutable). This document is serializable.- Since:
- 3.2.5
-
-
Constructor Details
-
BsonUtils
public BsonUtils()
-
-
Method Details
-
get
-
asMap
-
asMap
public static Map<String,Object> asMap(@Nullable org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry) Return theBsonobject asMap. Depending on the input type, the return value can be either a casted version ofbsonor a converted (detached from the original value) using the givenCodecRegistryto obtaincodecsthat might be required for conversion.- Parameters:
bson- can be null.codecRegistry- must not be null.- Returns:
- never null. Returns an empty
Mapif inputBsonis null. - Since:
- 4.0
-
asDocument
public static org.bson.Document asDocument(@Nullable org.bson.conversions.Bson bson) Return theBsonobject asDocument. Depending on the input type, the return value can be either a casted version ofbsonor a converted (detached from the original value).- Parameters:
bson-- Returns:
- Since:
- 3.2.5
-
asDocument
public static org.bson.Document asDocument(@Nullable org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry) Return theBsonobject asDocument. Depending on the input type, the return value can be either a casted version ofbsonor a converted (detached from the original value) using the givenCodecRegistryto obtaincodecsthat might be required for conversion.- Parameters:
bson-codecRegistry- must not be null.- Returns:
- never null.
- Since:
- 4.0
-
asMutableDocument
public static org.bson.Document asMutableDocument(org.bson.conversions.Bson bson) Return theBsonobject as mutableDocumentcontaining all entries fromBson.- Parameters:
bson-- Returns:
- a mutable
Documentcontaining all entries fromBson. - Since:
- 3.2.5
-
addToMap
-
addAllToMap
-
contains
Check if a given entry (key/value pair) is present in the givenBson.- Parameters:
bson- must not be null.key- must not be null.value- can be null.- Returns:
- true if (key/value pair) is present.
- Since:
- 3.2
-
removeNullId
public static boolean removeNullId(org.bson.conversions.Bson bson) Remove_id : nullfrom the givenBsonif present.- Parameters:
bson- must not be null.- Since:
- 3.2
-
toJavaType
-
simpleToBsonValue
- Parameters:
source- must not be null.- Returns:
- the corresponding
BsonValuerepresentation. - Throws:
IllegalArgumentException- if source does not correspond to aBsonValuetype.- Since:
- 3.0
-
simpleToBsonValue
@Contract("null, _ -> !null") public static org.bson.BsonValue simpleToBsonValue(@Nullable Object source, org.bson.codecs.configuration.CodecRegistry codecRegistry) - Parameters:
source- can be null.codecRegistry- TheCodecRegistryused as a fallback to convert types using nativeCodec. Must not be null.- Returns:
- the corresponding
BsonValuerepresentation. - Throws:
IllegalArgumentException- if source does not correspond to aBsonValuetype.- Since:
- 4.2
-
merge
public static org.bson.Document merge(org.bson.Document... documents) Merge the givendocumentsinto on in the given order. Keys contained within multiple documents are overwritten by their follow-ups.- Parameters:
documents- must not be null. Can be empty.- Returns:
- the document containing all key value pairs.
- Since:
- 2.2
-
toDocumentOrElse
-
toJson
Serialize the givenDocumentas Json applying default codecs if necessary.- Parameters:
source-- Returns:
- Since:
- 2.2.1
-
isJsonDocument
-
isJsonArray
-
parse
public static org.bson.Document parse(String json, @Nullable CodecRegistryProvider codecRegistryProvider) Parse the given json toDocumentapplying transformations as specified by a potentially givenCodec.- Parameters:
json- must not be null.codecRegistryProvider- can be null. In that case the defaultDocumentCodecis used.- Returns:
- never null.
- Throws:
IllegalArgumentException- if the required argument is null.- Since:
- 3.0
-
resolveValue
Resolve the value for a given key. If the givenBsonvalue contains the key the value is immediately returned. If not and the key contains a path using the dot (.) notation it will try to resolve the path by inspecting the individual parts. If one of the intermediate ones is null or cannot be inspected further (wrong) type, null is returned.- Parameters:
bson- the source to inspect. Must not be null.key- the key to lookup. Must not be null.- Returns:
- can be null.
- Since:
- 3.0.8
-
resolveValue
Resolve the value for a givenfield name. If the given name is aFieldName.Type.KEYthe value is obtained from the targetBsonimmediately. If the given fieldName is aFieldName.Type.PATHmaybe using the dot (.) notation it will try to resolve the path by inspecting the individual parts. If one of the intermediate ones is null or cannot be inspected further (wrong) type, null is returned.- Parameters:
bson- the source to inspect. Must not be null.fieldName- the name to lookup. Must not be null.- Returns:
- can be null.
- Since:
- 4.2
-
resolveValue
Resolve the value for a givenfield name. If the given name is aFieldName.Type.KEYthe value is obtained from the targetBsonimmediately. If the given fieldName is aFieldName.Type.PATHmaybe using the dot (.) notation it will try to resolve the path by inspecting the individual parts. If one of the intermediate ones is null or cannot be inspected further (wrong) type, null is returned.- Parameters:
source- the source to inspect. Must not be null.fieldName- the key to lookup. Must not be null.- Returns:
- can be null.
- Since:
- 4.2
-
resolveValue
Resolve the value for a given key. If the givenMapvalue contains the key the value is immediately returned. If not and the key contains a path using the dot (.) notation it will try to resolve the path by inspecting the individual parts. If one of the intermediate ones is null or cannot be inspected further (wrong) type, null is returned.- Parameters:
source- the source to inspect. Must not be null.key- the key to lookup. Must not be null.- Returns:
- can be null.
- Since:
- 4.1
-
hasValue
-
hasValue
Returns whether the underlyingbsonhas a value (null or non-null) for the givenkey.- Parameters:
bson- the source to inspect. Must not be null.key- the key to lookup. Must not be null.- Returns:
- true if no non null value present.
- Since:
- 3.0.8
-
asBson
Returns the given source object asBson, i.e.Documents and maps as is or throwIllegalArgumentException.- Parameters:
source-- Returns:
- the converted/casted source object.
- Throws:
IllegalArgumentException- ifsourcecannot be converted/cast toBson.- Since:
- 3.2.3
- See Also:
-
supportsBson
Returns the given source can be used/converted asBson.- Parameters:
source-- Returns:
- true if the given source can be converted to
Bson. - Since:
- 3.2.3
-
asCollection
Returns given object asCollection. Will return theCollectionas is if the source is aCollectionalready, will convert an array into aCollectionor simply create a single element collection for everything else.- Parameters:
source- must not be null.- Returns:
- never null.
- Since:
- 3.2
-
mapValues
public static org.bson.Document mapValues(org.bson.Document source, BiFunction<String, Object, Object> valueMapper) -
mapEntries
-