Class BsonUtils
java.lang.Object
org.springframework.data.mongodb.util.BsonUtils
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddAllToMap(Bson target, Map<String, ?> source) Add all entries from the given sourceMapto the target.static voidstatic BsonReturns the given source object asBson, i.e.static Collection<?> asCollection(Object source) Returns given object asCollection.static DocumentasDocument(@Nullable Bson bson) static DocumentasDocument(@Nullable Bson bson, CodecRegistry codecRegistry) asMap(@Nullable Bson bson, CodecRegistry codecRegistry) static DocumentasMutableDocument(Bson bson) 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 DocumentmapEntries(Document source, Function<Map.Entry<String, Object>, String> keyMapper, Function<Map.Entry<String, Object>, Object> valueMapper) static Documentstatic DocumentMerge the givendocumentsinto on in the given order.static Documentparse(String json, @Nullable CodecRegistryProvider codecRegistryProvider) static booleanremoveNullId(Bson bson) Remove_id : nullfrom the givenBsonif present.resolveValue(Map<String, Object> source, String key) Resolve the value for a given key.resolveValue(Map<String, Object> source, FieldName fieldName) Resolve the value for a givenfield name.resolveValue(Bson bson, String key) Resolve the value for a given key.resolveValue(Bson bson, FieldName fieldName) Resolve the value for a givenfield name.static BsonValuesimpleToBsonValue(@Nullable Object source) Convert a given simple value (eg.static BsonValuesimpleToBsonValue(@Nullable Object source, CodecRegistry codecRegistry) Convert a given simple value (eg.static booleansupportsBson(Object source) Returns the given source can be used/converted asBson.static DocumenttoDocumentOrElse(String source, Function<String, Document> orElse) static ObjecttoJavaType(BsonValue value) Extract the corresponding plain value fromBsonValue.Serialize the givenDocumentas Json applying default codecs if necessary.
-
Field Details
-
EMPTY_DOCUMENT
The empty document (immutable). This document is serializable.- Since:
- 3.2.5
-
-
Constructor Details
-
BsonUtils
public BsonUtils()
-
-
Method Details
-
get
-
asMap
-
asMap
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. -
asDocument
-
asDocument
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
-
addToMap
-
addAllToMap
-
contains
-
removeNullId
-
toJavaType
- Parameters:
value- must not be null.- Returns:
- Since:
- 2.1
-
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 BsonValue simpleToBsonValue(@Nullable Object source, 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
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
-
isJsonDocument
-
isJsonArray
-
parse
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
-
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
-
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
-
mapEntries
-