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
Modifier and TypeFieldDescriptionstatic final org.bson.Document
The empty document (immutable). -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addAllToMap
(org.bson.conversions.Bson target, Map<String, ?> source) Add all entries from the given sourceMap
to the target.static void
static org.bson.conversions.Bson
Returns the given source object asBson
, i.e.static Collection<?>
asCollection
(Object source) Returns given object asCollection
.static org.bson.Document
asDocument
(org.bson.conversions.Bson bson) Return theBson
object asDocument
.static org.bson.Document
asDocument
(org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry) Return theBson
object asDocument
.asMap
(org.bson.conversions.Bson bson) Return theBson
object asMap
.asMap
(org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry) Return theBson
object asMap
.static org.bson.Document
asMutableDocument
(org.bson.conversions.Bson bson) Return theBson
object as mutableDocument
containing all entries fromBson
.static boolean
Check if a given entry (key/value pair) is present in the givenBson
.static <T> T
static boolean
Returns whether the underlyingbson
has a value (null or non-null) for the givenkey
.static boolean
isJsonArray
(String value) Check if a given String looks likeparsable
json array.static boolean
isJsonDocument
(String value) Check if a given String looks likeparsable
json.static org.bson.Document
merge
(org.bson.Document... documents) Merge the givendocuments
into on in the given order.static org.bson.Document
parse
(String json, CodecRegistryProvider codecRegistryProvider) Parse the given json toDocument
applying transformations as specified by a potentially givenCodec
.static boolean
removeNullId
(org.bson.conversions.Bson bson) Remove_id : null
from the givenBson
if present.static Object
resolveValue
(org.bson.conversions.Bson bson, String key) Resolve a the value for a given key.static org.bson.BsonValue
simpleToBsonValue
(Object source) Convert a given simple value (eg.static boolean
supportsBson
(Object source) Returns the given source can be used/converted asBson
.static org.bson.Document
toDocumentOrElse
(String source, Function<String, org.bson.Document> orElse) static Object
toJavaType
(org.bson.BsonValue value) Extract the corresponding plain value fromBsonValue
.static String
toJson
(org.bson.Document source) Serialize the givenDocument
as 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
Return theBson
object asMap
. Depending on the input type, the return value can be either a casted version ofbson
or a converted (detached from the original value).- Parameters:
bson
-- Returns:
-
asMap
public static Map<String,Object> asMap(@Nullable org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry) Return theBson
object asMap
. Depending on the input type, the return value can be either a casted version ofbson
or a converted (detached from the original value) using the givenCodecRegistry
to obtaincodecs
that might be required for conversion.- Parameters:
bson
- can be null.codecRegistry
- must not be null.- Returns:
- never null. Returns an empty
Map
if inputBson
is null. - Since:
- 4.0
-
asDocument
public static org.bson.Document asDocument(org.bson.conversions.Bson bson) Return theBson
object asDocument
. Depending on the input type, the return value can be either a casted version ofbson
or a converted (detached from the original value).- Parameters:
bson
-- Returns:
- Since:
- 3.2.5
-
asDocument
public static org.bson.Document asDocument(org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry) Return theBson
object asDocument
. Depending on the input type, the return value can be either a casted version ofbson
or a converted (detached from the original value) using the givenCodecRegistry
to obtaincodecs
that 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 theBson
object as mutableDocument
containing all entries fromBson
.- Parameters:
bson
-- Returns:
- a mutable
Document
containing all entries fromBson
. - Since:
- 3.2.5
-
addToMap
-
addAllToMap
Add all entries from the given sourceMap
to the target.- Parameters:
target
- must not be null.source
- must not be null.- Since:
- 3.2
-
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 : null
from the givenBson
if present.- Parameters:
bson
- must not be null.- Since:
- 3.2
-
toJavaType
- Parameters:
value
- must not be null.- Returns:
- Since:
- 2.1
-
simpleToBsonValue
- Parameters:
source
- must not be null.- Returns:
- the corresponding
BsonValue
representation. - Throws:
IllegalArgumentException
- if source does not correspond to aBsonValue
type.- Since:
- 3.0
-
merge
public static org.bson.Document merge(org.bson.Document... documents) Merge the givendocuments
into 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
public static org.bson.Document toDocumentOrElse(String source, Function<String, org.bson.Document> orElse) - Parameters:
source
-orElse
-- Returns:
- Since:
- 2.2
-
toJson
Serialize the givenDocument
as Json applying default codecs if necessary.- Parameters:
source
-- Returns:
- Since:
- 2.2.1
-
isJsonDocument
Check if a given String looks likeparsable
json.- Parameters:
value
- can be null.- Returns:
- true if the given value looks like a json document.
- Since:
- 3.0
-
isJsonArray
Check if a given String looks likeparsable
json array.- Parameters:
value
- can be null.- Returns:
- true if the given value looks like a json array.
- Since:
- 3.0
-
parse
public static org.bson.Document parse(String json, @Nullable CodecRegistryProvider codecRegistryProvider) Parse the given json toDocument
applying transformations as specified by a potentially givenCodec
.- Parameters:
json
- must not be null.codecRegistryProvider
- can be null. In that case the defaultDocumentCodec
is used.- Returns:
- never null.
- Throws:
IllegalArgumentException
- if the required argument is null.- Since:
- 3.0
-
resolveValue
Resolve a the value for a given key. If the givenBson
value 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
-
hasValue
Returns whether the underlyingbson
has 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.Document
s and maps as is or throwIllegalArgumentException
.- Parameters:
source
-- Returns:
- the converted/casted source object.
- Throws:
IllegalArgumentException
- ifsource
cannot 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 theCollection
as is if the source is aCollection
already, will convert an array into aCollection
or simply create a single element collection for everything else.- Parameters:
source
- must not be null.- Returns:
- never null.
- Since:
- 3.2
-