Package org.bson

Class Document

  • All Implemented Interfaces:
    , <,​>, Bson

    public class Document
    extends 
    implements <,​>, , Bson
    A representation of a document as a Map. All iterators will traverse the elements in insertion order, as with LinkedHashMap.
    Since:
    3.0.0
    See Also:
    Serialized Form
    MongoDB documentation
    • Nested Class Summary

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

        < extends ,​ extends >
    • Constructor Summary

      Constructors 
      Constructor Description
      Document()
      Creates an empty Document instance.
       key,  value)
      Create a Document instance initialized with the given key/value pair.
      <,​> map)
      Creates a Document instance initialized with the given map.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Document  key,  value)
      Put the given key/value pair into this Document and return this.
      void clear()  
      boolean  key)  
      boolean  value)  
      <<,​>> entrySet()  
      boolean  o)  
       key)  
      <T> T  key, <T> clazz)
      Gets the value of the given key, casting it to the given Class<T>.
      <T> T  key, T defaultValue)
      Gets the value of the given key, casting it to Class<T> or returning the default value if null.
       key)
      Gets the value of the given key as a Boolean.
      boolean  key, boolean defaultValue)
      Gets the value of the given key as a primitive boolean.
       key)
      Gets the value of the given key as a Date.
       key)
      Gets the value of the given key as a Double.
      <T> T <?> keys, <T> clazz)
      Gets the value in an embedded document, casting it to the given Class<T>.
      <T> T <?> keys, T defaultValue)
      Gets the value in an embedded document, casting it to the given Class<T> or returning the default value if null.
       key)
      Gets the value of the given key as an Integer.
      int  key, int defaultValue)
      Gets the value of the given key as a primitive int.
      <T> <T>  key, <T> clazz)
      Gets the list value of the given key, casting the list elements to the given Class<T>.
      <T> <T>  key, <T> clazz, <T> defaultValue)
      Gets the list value of the given key, casting the list elements to Class<T> or returning the default list value if null.
       key)
      Gets the value of the given key as a Long.
      ObjectId  key)
      Gets the value of the given key as an ObjectId.
       key)
      Gets the value of the given key as a String.
      int hashCode()  
      boolean isEmpty()  
      <> keySet()  
      static Document  json)
      Parses a string in MongoDB Extended JSON format to a Document
      static Document  json, Decoder<Document> decoder)
      Parses a string in MongoDB Extended JSON format to a Document
       key,  value)  
      void <? extends ,​?> map)  
       key)  
      int size()  
      <C> BsonDocument <C> documentClass, CodecRegistry codecRegistry)
      Render the filter into a BsonDocument.
      toJson()
      Gets a JSON representation of this document using the JsonMode.STRICT output mode, and otherwise the default settings of JsonWriterSettings.Builder and DocumentCodec.
      toJson​(Encoder<Document> encoder)
      Gets a JSON representation of this document
      toJson​(JsonWriterSettings writerSettings)
      Gets a JSON representation of this document
      toJson​(JsonWriterSettings writerSettings, Encoder<Document> encoder)
      Gets a JSON representation of this document
      toString()  
      <> values()  
      • Methods inherited from class java.lang.

        , , , , , , ,
      • Methods inherited from interface java.util.

        , , , , , , , , , ,
    • Constructor Detail

      • Document

        public Document()
        Creates an empty Document instance.
      • Document

        public Document​( key,
                         value)
        Create a Document instance initialized with the given key/value pair.
        Parameters:
        key - key
        value - value
      • Document

        public Document​(<,​> map)
        Creates a Document instance initialized with the given map.
        Parameters:
        map - initial map
    • Method Detail

      • parse

        public static  json)
        Parses a string in MongoDB Extended JSON format to a Document
        Parameters:
        json - the JSON string
        Returns:
        a corresponding Document object
        See Also:
        JsonReader
        MongoDB documentation
      • parse

        public static  json,
                                     Decoder<Document> decoder)
        Parses a string in MongoDB Extended JSON format to a Document
        Parameters:
        json - the JSON string
        decoder - the Decoder to use to parse the JSON string into a Document
        Returns:
        a corresponding Document object
        See Also:
        JsonReader
        MongoDB documentation
      • toBsonDocument

        public <C> <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
      • append

        public  key,
                                value)
        Put the given key/value pair into this Document and return this. Useful for chaining puts in a single expression, e.g.
         doc.append("a", 1).append("b", 2)}
         
        Parameters:
        key - key
        value - value
        Returns:
        this
      • get

        public <T> T get​( key,
                         <T> clazz)
        Gets the value of the given key, casting it to the given Class<T>. This is useful to avoid having casts in client code, though the effect is the same. So to get the value of a key that is of type String, you would write String name = doc.get("name", String.class) instead of String name = (String) doc.get("x") .
        Type Parameters:
        T - the type of the class
        Parameters:
        key - the key
        clazz - the non-null class to cast the value to
        Returns:
        the value of the given key, or null if the instance does not contain this key.
        Throws:
        - if the value of the given key is not of type T
      • get

        public <T> T get​( key,
                         T defaultValue)
        Gets the value of the given key, casting it to Class<T> or returning the default value if null. This is useful to avoid having casts in client code, though the effect is the same.
        Type Parameters:
        T - the type of the class
        Parameters:
        key - the key
        defaultValue - what to return if the value is null
        Returns:
        the value of the given key, or null if the instance does not contain this key.
        Throws:
        - if the value of the given key is not of type T
        Since:
        3.5
      • getEmbedded

        public <T> T getEmbedded​(<?> keys,
                                 <T> clazz)
        Gets the value in an embedded document, casting it to the given Class<T>. The list of keys represents a path to the embedded value, drilling down into an embedded document for each key. This is useful to avoid having casts in client code, though the effect is the same. The generic type of the keys list is ? to be consistent with the corresponding get methods, but in practice the actual type of the argument should be List<String>. So to get the embedded value of a key list that is of type String, you would write String name = doc.getEmbedded(List.of("employee", "manager", "name"), String.class) instead of String name = (String) doc.get("employee", Document.class).get("manager", Document.class).get("name") .
        Type Parameters:
        T - the type of the class
        Parameters:
        keys - the list of keys
        clazz - the non-null class to cast the value to
        Returns:
        the value of the given embedded key, or null if the instance does not contain this embedded key.
        Throws:
        - if the value of the given embedded key is not of type T
        Since:
        3.10
      • getEmbedded

        public <T> T getEmbedded​(<?> keys,
                                 T defaultValue)
        Gets the value in an embedded document, casting it to the given Class<T> or returning the default value if null. The list of keys represents a path to the embedded value, drilling down into an embedded document for each key. This is useful to avoid having casts in client code, though the effect is the same. The generic type of the keys list is ? to be consistent with the corresponding get methods, but in practice the actual type of the argument should be List<String>. So to get the embedded value of a key list that is of type String, you would write String name = doc.getEmbedded(List.of("employee", "manager", "name"), "John Smith") instead of String name = doc.get("employee", Document.class).get("manager", Document.class).get("name", "John Smith") .
        Type Parameters:
        T - the type of the class
        Parameters:
        keys - the list of keys
        defaultValue - what to return if the value is null
        Returns:
        the value of the given key, or null if the instance does not contain this key.
        Throws:
        - if the value of the given key is not of type T
        Since:
        3.10
      • getInteger

        public  getInteger​( key)
        Gets the value of the given key as an Integer.
        Parameters:
        key - the key
        Returns:
        the value as an integer, which may be null
        Throws:
        - if the value is not an integer
      • getInteger

        public int getInteger​( key,
                              int defaultValue)
        Gets the value of the given key as a primitive int.
        Parameters:
        key - the key
        defaultValue - what to return if the value is null
        Returns:
        the value as an integer, which may be null
        Throws:
        - if the value is not an integer
      • getLong

        public  getLong​( key)
        Gets the value of the given key as a Long.
        Parameters:
        key - the key
        Returns:
        the value as a long, which may be null
        Throws:
        - if the value is not an long
      • getDouble

        public  getDouble​( key)
        Gets the value of the given key as a Double.
        Parameters:
        key - the key
        Returns:
        the value as a double, which may be null
        Throws:
        - if the value is not an double
      • getString

        public  getString​( key)
        Gets the value of the given key as a String.
        Parameters:
        key - the key
        Returns:
        the value as a String, which may be null
        Throws:
        - if the value is not a String
      • getBoolean

        public  getBoolean​( key)
        Gets the value of the given key as a Boolean.
        Parameters:
        key - the key
        Returns:
        the value as a Boolean, which may be null
        Throws:
        - if the value is not an boolean
      • getBoolean

        public boolean getBoolean​( key,
                                  boolean defaultValue)
        Gets the value of the given key as a primitive boolean.
        Parameters:
        key - the key
        defaultValue - what to return if the value is null
        Returns:
        the value as a primitive boolean
        Throws:
        - if the value is not a boolean
      • getObjectId

        public  key)
        Gets the value of the given key as an ObjectId.
        Parameters:
        key - the key
        Returns:
        the value as an ObjectId, which may be null
        Throws:
        - if the value is not an ObjectId
      • getDate

        public  getDate​( key)
        Gets the value of the given key as a Date.
        Parameters:
        key - the key
        Returns:
        the value as a Date, which may be null
        Throws:
        - if the value is not a Date
      • getList

        public <T> <T> getList​( key,
                                   <T> clazz)
        Gets the list value of the given key, casting the list elements to the given Class<T>. This is useful to avoid having casts in client code, though the effect is the same.
        Type Parameters:
        T - the type of the class
        Parameters:
        key - the key
        clazz - the non-null class to cast the list value to
        Returns:
        the list value of the given key, or null if the instance does not contain this key.
        Throws:
        - if the elements in the list value of the given key is not of type T or the value is not a list
        Since:
        3.10
      • getList

        public <T> <T> getList​( key,
                                   <T> clazz,
                                   <T> defaultValue)
        Gets the list value of the given key, casting the list elements to Class<T> or returning the default list value if null. This is useful to avoid having casts in client code, though the effect is the same.
        Type Parameters:
        T - the type of the class
        Parameters:
        key - the key
        clazz - the non-null class to cast the list value to
        defaultValue - what to return if the value is null
        Returns:
        the list value of the given key, or the default list value if the instance does not contain this key.
        Throws:
        - if the value of the given key is not of type T
        Since:
        3.10
      • toJson

        public  toJson​(JsonWriterSettings writerSettings)
        Gets a JSON representation of this document

        With the default DocumentCodec.

        Parameters:
        writerSettings - the json writer settings to use when encoding
        Returns:
        a JSON representation of this document
        Throws:
        CodecConfigurationException - if the document contains types not in the default registry
      • toJson

        public  toJson​(Encoder<Document> encoder)
        Gets a JSON representation of this document

        With the default JsonWriterSettings.

        Parameters:
        encoder - the document codec instance to use to encode the document
        Returns:
        a JSON representation of this document
        Throws:
        CodecConfigurationException - if the registry does not contain a codec for the document values.
      • toJson

        public  toJson​(JsonWriterSettings writerSettings,
                             Encoder<Document> encoder)
        Gets a JSON representation of this document
        Parameters:
        writerSettings - the json writer settings to use when encoding
        encoder - the document codec instance to use to encode the document
        Returns:
        a JSON representation of this document
        Throws:
        CodecConfigurationException - if the registry does not contain a codec for the document values.
      • size

        public int size()
        Specified by:
         in interface <,​>
      • isEmpty

        public boolean isEmpty()
        Specified by:
         in interface <,​>
      • containsValue

        public boolean containsValue​( value)
        Specified by:
         in interface <,​>
      • containsKey

        public boolean containsKey​( key)
        Specified by:
         in interface <,​>
      • get

        public  get​( key)
        Specified by:
         in interface <,​>
      • put

        public  put​( key,
                           value)
        Specified by:
         in interface <,​>
      • remove

        public  remove​( key)
        Specified by:
         in interface <,​>
      • putAll

        public void putAll​(<? extends ,​?> map)
        Specified by:
         in interface <,​>
      • clear

        public void clear()
        Specified by:
         in interface <,​>
      • keySet

        public <> keySet()
        Specified by:
         in interface <,​>
      • values

        public <> values()
        Specified by:
         in interface <,​>
      • entrySet

        public <<,​>> entrySet()
        Specified by:
         in interface <,​>
      • equals

        public boolean equals​( o)
        Specified by:
         in interface <,​>
        Overrides:
         in class 
      • hashCode

        public int hashCode()
        Specified by:
         in interface <,​>
        Overrides:
         in class 
      • toString

        public  toString()
        Overrides:
         in class