Package org.bson

Class BsonDocument

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.String,​BsonValue>, Bson
    Direct Known Subclasses:
    BsonDocumentWrapper, RawBsonDocument

    public class BsonDocument
    extends BsonValue
    implements java.util.Map<java.lang.String,​BsonValue>, java.lang.Cloneable, Bson, java.io.Serializable
    A type-safe container for a BSON document. This class should NOT be sub-classed by third parties.
    Since:
    3.0
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      BsonDocument()
      Construct an empty document.
      BsonDocument​(java.lang.String key, BsonValue value)
      Construct a new instance with a single key value pair
      BsonDocument​(java.util.List<BsonElement> bsonElements)
      Construct a new instance with the given list BsonElement, none of which may be null.
    • Constructor Detail

      • BsonDocument

        public BsonDocument​(java.util.List<BsonElement> bsonElements)
        Construct a new instance with the given list BsonElement, none of which may be null.
        Parameters:
        bsonElements - a list of BsonElement
      • BsonDocument

        public BsonDocument​(java.lang.String key,
                            BsonValue value)
        Construct a new instance with a single key value pair
        Parameters:
        key - the key
        value - the value
      • BsonDocument

        public BsonDocument()
        Construct an empty document.
    • Method Detail

      • parse

        public static BsonDocument parse​(java.lang.String json)
        Parses a string in MongoDB Extended JSON format to a BsonDocument
        Parameters:
        json - the JSON string
        Returns:
        a corresponding BsonDocument object
        See Also:
        JsonReader
      • toBsonDocument

        public <C> BsonDocument toBsonDocument​(java.lang.Class<C> documentClass,
                                               CodecRegistry codecRegistry)
        Description copied from interface: Bson
        Render the filter into a BsonDocument.
        Specified by:
        toBsonDocument in interface Bson
        Type Parameters:
        C - the type of the document class
        Parameters:
        documentClass - the document class in scope for the collection. This parameter may be ignored, but it may be used to alter the structure of the returned BsonDocument based on some knowledge of the document class.
        codecRegistry - the codec registry. This parameter may be ignored, but it may be used to look up Codec instances for the document class or any other related class.
        Returns:
        the BsonDocument
      • getBsonType

        public BsonType getBsonType()
        Description copied from class: BsonValue
        Gets the BSON type of this value.
        Specified by:
        getBsonType in class BsonValue
        Returns:
        the BSON type, which may not be null (but may be BSONType.NULL)
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<java.lang.String,​BsonValue>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<java.lang.String,​BsonValue>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<java.lang.String,​BsonValue>
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<java.lang.String,​BsonValue>
      • get

        public BsonValue get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<java.lang.String,​BsonValue>
      • getDocument

        public BsonDocument getDocument​(java.lang.Object key)
        Gets the value of the key if it is a BsonDocument, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonDocument
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not a BsonDocument
      • getArray

        public BsonArray getArray​(java.lang.Object key)
        Gets the value of the key if it is a BsonArray, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonArray
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getNumber

        public BsonNumber getNumber​(java.lang.Object key)
        Gets the value of the key if it is a BsonNumber, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonNumber
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getInt32

        public BsonInt32 getInt32​(java.lang.Object key)
        Gets the value of the key if it is a BsonInt32, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonInt32
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getInt64

        public BsonInt64 getInt64​(java.lang.Object key)
        Gets the value of the key if it is a BsonInt64, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonInt64
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getDecimal128

        public BsonDecimal128 getDecimal128​(java.lang.Object key)
        Gets the value of the key if it is a BsonDecimal128, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonDecimal128
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
        Since:
        3.4
      • getDouble

        public BsonDouble getDouble​(java.lang.Object key)
        Gets the value of the key if it is a BsonDouble, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonDouble
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getBoolean

        public BsonBoolean getBoolean​(java.lang.Object key)
        Gets the value of the key if it is a BsonBoolean, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonBoolean
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getString

        public BsonString getString​(java.lang.Object key)
        Gets the value of the key if it is a BsonString, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonString
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getDateTime

        public BsonDateTime getDateTime​(java.lang.Object key)
        Gets the value of the key if it is a BsonDateTime, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonDateTime
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getTimestamp

        public BsonTimestamp getTimestamp​(java.lang.Object key)
        Gets the value of the key if it is a BsonTimestamp, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonTimestamp
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getObjectId

        public BsonObjectId getObjectId​(java.lang.Object key)
        Gets the value of the key if it is a BsonObjectId, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonObjectId
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getRegularExpression

        public BsonRegularExpression getRegularExpression​(java.lang.Object key)
        Gets the value of the key if it is a BsonRegularExpression, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonRegularExpression
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • getBinary

        public BsonBinary getBinary​(java.lang.Object key)
        Gets the value of the key if it is a BsonBinary, or throws if not.
        Parameters:
        key - the key
        Returns:
        the value of the key as a BsonBinary
        Throws:
        BsonInvalidOperationException - if the document does not contain the key or the value is not of the expected type
      • isNull

        public boolean isNull​(java.lang.Object key)
        Returns true if the value of the key is a BsonNull, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonNull, returns false if the document does not contain the key.
      • isDocument

        public boolean isDocument​(java.lang.Object key)
        Returns true if the value of the key is a BsonDocument, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonDocument, returns false if the document does not contain the key.
      • isArray

        public boolean isArray​(java.lang.Object key)
        Returns true if the value of the key is a BsonArray, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonArray, returns false if the document does not contain the key.
      • isNumber

        public boolean isNumber​(java.lang.Object key)
        Returns true if the value of the key is a BsonNumber, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonNumber, returns false if the document does not contain the key.
      • isInt32

        public boolean isInt32​(java.lang.Object key)
        Returns true if the value of the key is a BsonInt32, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonInt32, returns false if the document does not contain the key.
      • isInt64

        public boolean isInt64​(java.lang.Object key)
        Returns true if the value of the key is a BsonInt64, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonInt64, returns false if the document does not contain the key.
      • isDecimal128

        public boolean isDecimal128​(java.lang.Object key)
        Returns true if the value of the key is a BsonDecimal128, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonDecimal128, returns false if the document does not contain the key.
        Since:
        3.4
      • isDouble

        public boolean isDouble​(java.lang.Object key)
        Returns true if the value of the key is a BsonDouble, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonDouble, returns false if the document does not contain the key.
      • isBoolean

        public boolean isBoolean​(java.lang.Object key)
        Returns true if the value of the key is a BsonBoolean, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonBoolean, returns false if the document does not contain the key.
      • isString

        public boolean isString​(java.lang.Object key)
        Returns true if the value of the key is a BsonString, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonString, returns false if the document does not contain the key.
      • isDateTime

        public boolean isDateTime​(java.lang.Object key)
        Returns true if the value of the key is a BsonDateTime, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonDateTime, returns false if the document does not contain the key.
      • isTimestamp

        public boolean isTimestamp​(java.lang.Object key)
        Returns true if the value of the key is a BsonTimestamp, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonTimestamp, returns false if the document does not contain the key.
      • isObjectId

        public boolean isObjectId​(java.lang.Object key)
        Returns true if the value of the key is a BsonObjectId, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonObjectId, returns false if the document does not contain the key.
      • isBinary

        public boolean isBinary​(java.lang.Object key)
        Returns true if the value of the key is a BsonBinary, returns false if the document does not contain the key.
        Parameters:
        key - the key
        Returns:
        true if the value of the key is a BsonBinary, returns false if the document does not contain the key.
      • get

        public BsonValue get​(java.lang.Object key,
                             BsonValue defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonValue
      • getDocument

        public BsonDocument getDocument​(java.lang.Object key,
                                        BsonDocument defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonDocument.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonDocument
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getArray

        public BsonArray getArray​(java.lang.Object key,
                                  BsonArray defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonArray.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonArray
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getNumber

        public BsonNumber getNumber​(java.lang.Object key,
                                    BsonNumber defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonNumber.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonNumber
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getInt32

        public BsonInt32 getInt32​(java.lang.Object key,
                                  BsonInt32 defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonInt32.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonInt32
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getInt64

        public BsonInt64 getInt64​(java.lang.Object key,
                                  BsonInt64 defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonInt64.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonInt64
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getDecimal128

        public BsonDecimal128 getDecimal128​(java.lang.Object key,
                                            BsonDecimal128 defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonDecimal128.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonDecimal128
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
        Since:
        3.4
      • getDouble

        public BsonDouble getDouble​(java.lang.Object key,
                                    BsonDouble defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonDouble.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonDouble
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getBoolean

        public BsonBoolean getBoolean​(java.lang.Object key,
                                      BsonBoolean defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonBoolean.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonBoolean
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getString

        public BsonString getString​(java.lang.Object key,
                                    BsonString defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonString.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonString
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getDateTime

        public BsonDateTime getDateTime​(java.lang.Object key,
                                        BsonDateTime defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonDateTime.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonDateTime
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getTimestamp

        public BsonTimestamp getTimestamp​(java.lang.Object key,
                                          BsonTimestamp defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonTimestamp.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonTimestamp
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getObjectId

        public BsonObjectId getObjectId​(java.lang.Object key,
                                        BsonObjectId defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonObjectId.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonObjectId
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getBinary

        public BsonBinary getBinary​(java.lang.Object key,
                                    BsonBinary defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonBinary.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonBinary
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • getRegularExpression

        public BsonRegularExpression getRegularExpression​(java.lang.Object key,
                                                          BsonRegularExpression defaultValue)
        If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key as a BsonRegularExpression.
        Parameters:
        key - the key
        defaultValue - the default value
        Returns:
        the value of the key as a BsonRegularExpression
        Throws:
        BsonInvalidOperationException - if the document contains the key but the value is not of the expected type
      • put

        public BsonValue put​(java.lang.String key,
                             BsonValue value)
        Specified by:
        put in interface java.util.Map<java.lang.String,​BsonValue>
      • remove

        public BsonValue remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<java.lang.String,​BsonValue>
      • putAll

        public void putAll​(java.util.Map<? extends java.lang.String,​? extends BsonValue> m)
        Specified by:
        putAll in interface java.util.Map<java.lang.String,​BsonValue>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<java.lang.String,​BsonValue>
      • keySet

        public java.util.Set<java.lang.String> keySet()
        Specified by:
        keySet in interface java.util.Map<java.lang.String,​BsonValue>
      • values

        public java.util.Collection<BsonValue> values()
        Specified by:
        values in interface java.util.Map<java.lang.String,​BsonValue>
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​BsonValue>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<java.lang.String,​BsonValue>
      • append

        public BsonDocument append​(java.lang.String key,
                                   BsonValue value)
        Put the given key and value into this document, and return the document.
        Parameters:
        key - the key
        value - the value
        Returns:
        this
      • getFirstKey

        public java.lang.String getFirstKey()
        Gets the first key in the document.
        Returns:
        the first key in the document
        Throws:
        java.util.NoSuchElementException - if the document is empty
        Since:
        3.6
      • asBsonReader

        public BsonReader asBsonReader()
        Gets the first value in the document
        Returns:
        the first value in the document
        Throws:
        java.util.NoSuchElementException - if the document is empty
        Since:
        3.7
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<java.lang.String,​BsonValue>
        Overrides:
        hashCode in class java.lang.Object
      • toJson

        public java.lang.String toJson​(JsonWriterSettings settings)
        Gets a JSON representation of this document using the given JsonWriterSettings.
        Parameters:
        settings - the JSON writer settings
        Returns:
        a JSON representation of this document
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object