Class BsonUtils

java.lang.Object
org.springframework.data.mongodb.util.BsonUtils

public class BsonUtils extends Object
Internal API for operations on Bson elements that can be either Document or DBObject.
Since:
2.0
Author:
Christoph Strobl, Mark Paluch
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final org.bson.Document
    The empty document (immutable).
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addAllToMap(org.bson.conversions.Bson target, Map<String,?> source)
    Add all entries from the given source Map to the target.
    static void
    addToMap(org.bson.conversions.Bson bson, String key, Object value)
     
    static org.bson.conversions.Bson
    asBson(Object source)
    Returns the given source object as Bson, i.e.
    static Collection<?>
    Returns given object as Collection.
    static org.bson.Document
    asDocument(org.bson.conversions.Bson bson)
    Return the Bson object as Document.
    static org.bson.Document
    asDocument(org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry)
    Return the Bson object as Document.
    static Map<String,Object>
    asMap(org.bson.conversions.Bson bson)
    Return the Bson object as Map.
    static Map<String,Object>
    asMap(org.bson.conversions.Bson bson, org.bson.codecs.configuration.CodecRegistry codecRegistry)
    Return the Bson object as Map.
    static org.bson.Document
    asMutableDocument(org.bson.conversions.Bson bson)
    Return the Bson object as mutable Document containing all entries from Bson.
    static boolean
    contains(org.bson.conversions.Bson bson, String key, Object value)
    Check if a given entry (key/value pair) is present in the given Bson.
    static <T> T
    get(org.bson.conversions.Bson bson, String key)
     
    static boolean
    hasValue(org.bson.conversions.Bson bson, String key)
    Returns whether the underlying bson has a value (null or non-null) for the given key.
    static boolean
    hasValue(org.bson.conversions.Bson bson, FieldName fieldName)
     
    static boolean
    Check if a given String looks like parsable json array.
    static boolean
    Check if a given String looks like parsable json.
    static org.bson.Document
    merge(org.bson.Document... documents)
    Merge the given documents into on in the given order.
    static org.bson.Document
    parse(String json, CodecRegistryProvider codecRegistryProvider)
    Parse the given json to Document applying transformations as specified by a potentially given Codec.
    static boolean
    removeNullId(org.bson.conversions.Bson bson)
    Remove _id : null from the given Bson if present.
    static Object
    Resolve the value for a given key.
    static Object
    resolveValue(Map<String,Object> source, FieldName fieldName)
    Resolve the value for a given field name.
    static Object
    resolveValue(org.bson.conversions.Bson bson, String key)
    Resolve the value for a given key.
    static Object
    resolveValue(org.bson.conversions.Bson bson, FieldName fieldName)
    Resolve the value for a given field name.
    static org.bson.BsonValue
    Convert a given simple value (eg.
    static org.bson.BsonValue
    simpleToBsonValue(Object source, org.bson.codecs.configuration.CodecRegistry codecRegistry)
    Convert a given simple value (eg.
    static boolean
    Returns the given source can be used/converted as Bson.
    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 from BsonValue.
    static String
    toJson(org.bson.Document source)
    Serialize the given Document as Json applying default codecs if necessary.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EMPTY_DOCUMENT

      public static final org.bson.Document EMPTY_DOCUMENT
      The empty document (immutable). This document is serializable.
      Since:
      3.2.5
  • Constructor Details

    • BsonUtils

      public BsonUtils()
  • Method Details

    • get

      @Nullable public static <T> T get(org.bson.conversions.Bson bson, String key)
    • asMap

      public static Map<String,Object> asMap(org.bson.conversions.Bson bson)
      Return the Bson object as Map. Depending on the input type, the return value can be either a casted version of bson 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 the Bson object as Map. Depending on the input type, the return value can be either a casted version of bson or a converted (detached from the original value) using the given CodecRegistry to obtain codecs that might be required for conversion.
      Parameters:
      bson - can be null.
      codecRegistry - must not be null.
      Returns:
      never null. Returns an empty Map if input Bson is null.
      Since:
      4.0
    • asDocument

      public static org.bson.Document asDocument(org.bson.conversions.Bson bson)
      Return the Bson object as Document. Depending on the input type, the return value can be either a casted version of bson 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 the Bson object as Document. Depending on the input type, the return value can be either a casted version of bson or a converted (detached from the original value) using the given CodecRegistry to obtain codecs 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 the Bson object as mutable Document containing all entries from Bson.
      Parameters:
      bson -
      Returns:
      a mutable Document containing all entries from Bson.
      Since:
      3.2.5
    • addToMap

      public static void addToMap(org.bson.conversions.Bson bson, String key, @Nullable Object value)
    • addAllToMap

      public static void addAllToMap(org.bson.conversions.Bson target, Map<String,?> source)
      Add all entries from the given source Map to the target.
      Parameters:
      target - must not be null.
      source - must not be null.
      Since:
      3.2
    • contains

      public static boolean contains(org.bson.conversions.Bson bson, String key, @Nullable Object value)
      Check if a given entry (key/value pair) is present in the given Bson.
      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 given Bson if present.
      Parameters:
      bson - must not be null.
      Since:
      3.2
    • toJavaType

      public static Object toJavaType(org.bson.BsonValue value)
      Extract the corresponding plain value from BsonValue. Eg. plain String from BsonString.
      Parameters:
      value - must not be null.
      Returns:
      Since:
      2.1
    • simpleToBsonValue

      public static org.bson.BsonValue simpleToBsonValue(Object source)
      Convert a given simple value (eg. String, Long) to its corresponding BsonValue.
      Parameters:
      source - must not be null.
      Returns:
      the corresponding BsonValue representation.
      Throws:
      IllegalArgumentException - if source does not correspond to a BsonValue type.
      Since:
      3.0
    • simpleToBsonValue

      public static org.bson.BsonValue simpleToBsonValue(Object source, org.bson.codecs.configuration.CodecRegistry codecRegistry)
      Convert a given simple value (eg. String, Long) to its corresponding BsonValue.
      Parameters:
      source - must not be null.
      codecRegistry - The CodecRegistry used as a fallback to convert types using native Codec. Must not be null.
      Returns:
      the corresponding BsonValue representation.
      Throws:
      IllegalArgumentException - if source does not correspond to a BsonValue type.
      Since:
      4.2
    • merge

      public static org.bson.Document merge(org.bson.Document... documents)
      Merge the given documents 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

      @Nullable public static String toJson(@Nullable org.bson.Document source)
      Serialize the given Document as Json applying default codecs if necessary.
      Parameters:
      source -
      Returns:
      Since:
      2.2.1
    • isJsonDocument

      public static boolean isJsonDocument(@Nullable String value)
      Check if a given String looks like parsable json.
      Parameters:
      value - can be null.
      Returns:
      true if the given value looks like a json document.
      Since:
      3.0
    • isJsonArray

      public static boolean isJsonArray(@Nullable String value)
      Check if a given String looks like parsable 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 to Document applying transformations as specified by a potentially given Codec.
      Parameters:
      json - must not be null.
      codecRegistryProvider - can be null. In that case the default DocumentCodec is used.
      Returns:
      never null.
      Throws:
      IllegalArgumentException - if the required argument is null.
      Since:
      3.0
    • resolveValue

      @Nullable public static Object resolveValue(org.bson.conversions.Bson bson, String key)
      Resolve the value for a given key. If the given Bson 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
    • resolveValue

      public static Object resolveValue(org.bson.conversions.Bson bson, FieldName fieldName)
      Resolve the value for a given field name. If the given name is a FieldName.Type.KEY the value is obtained from the target Bson immediately. If the given fieldName is a FieldName.Type.PATH maybe 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

      @Nullable public static Object resolveValue(Map<String,Object> source, FieldName fieldName)
      Resolve the value for a given field name. If the given name is a FieldName.Type.KEY the value is obtained from the target Bson immediately. If the given fieldName is a FieldName.Type.PATH maybe 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

      @Nullable public static Object resolveValue(Map<String,Object> source, String key)
      Resolve the value for a given key. If the given Map 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:
      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

      public static boolean hasValue(org.bson.conversions.Bson bson, FieldName fieldName)
    • hasValue

      public static boolean hasValue(org.bson.conversions.Bson bson, String key)
      Returns whether the underlying bson has a value (null or non-null) for the given key.
      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

      public static org.bson.conversions.Bson asBson(Object source)
      Returns the given source object as Bson, i.e. Documents and maps as is or throw IllegalArgumentException.
      Parameters:
      source -
      Returns:
      the converted/casted source object.
      Throws:
      IllegalArgumentException - if source cannot be converted/cast to Bson.
      Since:
      3.2.3
      See Also:
    • supportsBson

      public static boolean supportsBson(Object source)
      Returns the given source can be used/converted as Bson.
      Parameters:
      source -
      Returns:
      true if the given source can be converted to Bson.
      Since:
      3.2.3
    • asCollection

      public static Collection<?> asCollection(Object source)
      Returns given object as Collection. Will return the Collection as is if the source is a Collection already, will convert an array into a Collection or simply create a single element collection for everything else.
      Parameters:
      source - must not be null.
      Returns:
      never null.
      Since:
      3.2