Package org.bson
Class Document
- java.lang.Object
-
- org.bson.Document
-
- All Implemented Interfaces:
java.io.Serializable
,java.util.Map<java.lang.String,java.lang.Object>
,Bson
public class Document extends java.lang.Object implements java.util.Map<java.lang.String,java.lang.Object>, java.io.Serializable, Bson
A representation of a document as aMap
. All iterators will traverse the elements in insertion order, as withLinkedHashMap
.- Since:
- 3.0.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Document()
Creates an empty Document instance.Document(java.lang.String key, java.lang.Object value)
Create a Document instance initialized with the given key/value pair.Document(java.util.Map<java.lang.String,java.lang.Object> 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
append(java.lang.String key, java.lang.Object value)
Put the given key/value pair into this Document and return this.void
clear()
boolean
containsKey(java.lang.Object key)
boolean
containsValue(java.lang.Object value)
java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>>
entrySet()
boolean
equals(java.lang.Object o)
java.lang.Object
get(java.lang.Object key)
<T> T
get(java.lang.Object key, java.lang.Class<T> clazz)
Gets the value of the given key, casting it to the givenClass<T>
.<T> T
get(java.lang.Object key, T defaultValue)
Gets the value of the given key, casting it toClass<T>
or returning the default value if null.java.lang.Boolean
getBoolean(java.lang.Object key)
Gets the value of the given key as a Boolean.boolean
getBoolean(java.lang.Object key, boolean defaultValue)
Gets the value of the given key as a primitive boolean.java.util.Date
getDate(java.lang.Object key)
Gets the value of the given key as a Date.java.lang.Double
getDouble(java.lang.Object key)
Gets the value of the given key as a Double.<T> T
getEmbedded(java.util.List<?> keys, java.lang.Class<T> clazz)
Gets the value in an embedded document, casting it to the givenClass<T>
.<T> T
getEmbedded(java.util.List<?> keys, T defaultValue)
Gets the value in an embedded document, casting it to the givenClass<T>
or returning the default value if null.java.lang.Integer
getInteger(java.lang.Object key)
Gets the value of the given key as an Integer.int
getInteger(java.lang.Object key, int defaultValue)
Gets the value of the given key as a primitive int.<T> java.util.List<T>
getList(java.lang.Object key, java.lang.Class<T> clazz)
Gets the list value of the given key, casting the list elements to the givenClass<T>
.<T> java.util.List<T>
getList(java.lang.Object key, java.lang.Class<T> clazz, java.util.List<T> defaultValue)
Gets the list value of the given key, casting the list elements toClass<T>
or returning the default list value if null.java.lang.Long
getLong(java.lang.Object key)
Gets the value of the given key as a Long.ObjectId
getObjectId(java.lang.Object key)
Gets the value of the given key as an ObjectId.java.lang.String
getString(java.lang.Object key)
Gets the value of the given key as a String.int
hashCode()
boolean
isEmpty()
java.util.Set<java.lang.String>
keySet()
static Document
parse(java.lang.String json)
Parses a string in MongoDB Extended JSON format to aDocument
static Document
parse(java.lang.String json, Decoder<Document> decoder)
Parses a string in MongoDB Extended JSON format to aDocument
java.lang.Object
put(java.lang.String key, java.lang.Object value)
void
putAll(java.util.Map<? extends java.lang.String,?> map)
java.lang.Object
remove(java.lang.Object key)
int
size()
<C> BsonDocument
toBsonDocument(java.lang.Class<C> documentClass, CodecRegistry codecRegistry)
Render the filter into a BsonDocument.java.lang.String
toJson()
Gets a JSON representation of this document using theJsonMode.STRICT
output mode, and otherwise the default settings ofJsonWriterSettings.Builder
andDocumentCodec
.java.lang.String
toJson(Encoder<Document> encoder)
Gets a JSON representation of this documentjava.lang.String
toJson(JsonWriterSettings writerSettings)
Gets a JSON representation of this documentjava.lang.String
toJson(JsonWriterSettings writerSettings, Encoder<Document> encoder)
Gets a JSON representation of this documentjava.lang.String
toString()
java.util.Collection<java.lang.Object>
values()
-
-
-
Constructor Detail
-
Document
public Document()
Creates an empty Document instance.
-
Document
public Document(java.lang.String key, java.lang.Object value)
Create a Document instance initialized with the given key/value pair.- Parameters:
key
- keyvalue
- value
-
Document
public Document(java.util.Map<java.lang.String,java.lang.Object> map)
Creates a Document instance initialized with the given map.- Parameters:
map
- initial map
-
-
Method Detail
-
parse
public static Document parse(java.lang.String json)
Parses a string in MongoDB Extended JSON format to aDocument
- Parameters:
json
- the JSON string- Returns:
- a corresponding
Document
object - See Also:
JsonReader
-
parse
public static Document parse(java.lang.String json, Decoder<Document> decoder)
Parses a string in MongoDB Extended JSON format to aDocument
- Parameters:
json
- the JSON stringdecoder
- theDecoder
to use to parse the JSON string into aDocument
- Returns:
- a corresponding
Document
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 interfaceBson
- 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 returnedBsonDocument
based on some knowledge of the document class.codecRegistry
- the codec registry. This parameter may be ignored, but it may be used to look upCodec
instances for the document class or any other related class.- Returns:
- the BsonDocument
-
append
public Document append(java.lang.String key, java.lang.Object 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
- keyvalue
- value- Returns:
- this
-
get
public <T> T get(java.lang.Object key, java.lang.Class<T> clazz)
Gets the value of the given key, casting it to the givenClass<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 writeString name = doc.get("name", String.class)
instead ofString name = (String) doc.get("x")
.- Type Parameters:
T
- the type of the class- Parameters:
key
- the keyclazz
- 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:
java.lang.ClassCastException
- if the value of the given key is not of type T
-
get
public <T> T get(java.lang.Object key, T defaultValue)
Gets the value of the given key, casting it toClass<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 keydefaultValue
- 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:
java.lang.ClassCastException
- if the value of the given key is not of type T- Since:
- 3.5
-
getEmbedded
public <T> T getEmbedded(java.util.List<?> keys, java.lang.Class<T> clazz)
Gets the value in an embedded document, casting it to the givenClass<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 correspondingget
methods, but in practice the actual type of the argument should beList<String>
. So to get the embedded value of a key list that is of type String, you would writeString name = doc.getEmbedded(List.of("employee", "manager", "name"), String.class)
instead ofString 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 keysclazz
- 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:
java.lang.ClassCastException
- if the value of the given embedded key is not of type T- Since:
- 3.10
-
getEmbedded
public <T> T getEmbedded(java.util.List<?> keys, T defaultValue)
Gets the value in an embedded document, casting it to the givenClass<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 correspondingget
methods, but in practice the actual type of the argument should beList<String>
. So to get the embedded value of a key list that is of type String, you would writeString name = doc.getEmbedded(List.of("employee", "manager", "name"), "John Smith")
instead ofString 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 keysdefaultValue
- 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:
java.lang.ClassCastException
- if the value of the given key is not of type T- Since:
- 3.10
-
getInteger
public java.lang.Integer getInteger(java.lang.Object 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:
java.lang.ClassCastException
- if the value is not an integer
-
getInteger
public int getInteger(java.lang.Object key, int defaultValue)
Gets the value of the given key as a primitive int.- Parameters:
key
- the keydefaultValue
- what to return if the value is null- Returns:
- the value as an integer, which may be null
- Throws:
java.lang.ClassCastException
- if the value is not an integer
-
getLong
public java.lang.Long getLong(java.lang.Object 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:
java.lang.ClassCastException
- if the value is not an long
-
getDouble
public java.lang.Double getDouble(java.lang.Object 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:
java.lang.ClassCastException
- if the value is not an double
-
getString
public java.lang.String getString(java.lang.Object 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:
java.lang.ClassCastException
- if the value is not a String
-
getBoolean
public java.lang.Boolean getBoolean(java.lang.Object 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:
java.lang.ClassCastException
- if the value is not an boolean
-
getBoolean
public boolean getBoolean(java.lang.Object key, boolean defaultValue)
Gets the value of the given key as a primitive boolean.- Parameters:
key
- the keydefaultValue
- what to return if the value is null- Returns:
- the value as a primitive boolean
- Throws:
java.lang.ClassCastException
- if the value is not a boolean
-
getObjectId
public ObjectId getObjectId(java.lang.Object 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:
java.lang.ClassCastException
- if the value is not an ObjectId
-
getDate
public java.util.Date getDate(java.lang.Object 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:
java.lang.ClassCastException
- if the value is not a Date
-
getList
public <T> java.util.List<T> getList(java.lang.Object key, java.lang.Class<T> clazz)
Gets the list value of the given key, casting the list elements to the givenClass<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 keyclazz
- 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:
java.lang.ClassCastException
- 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> java.util.List<T> getList(java.lang.Object key, java.lang.Class<T> clazz, java.util.List<T> defaultValue)
Gets the list value of the given key, casting the list elements toClass<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 keyclazz
- the non-null class to cast the list value todefaultValue
- 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:
java.lang.ClassCastException
- if the value of the given key is not of type T- Since:
- 3.10
-
toJson
public java.lang.String toJson()
Gets a JSON representation of this document using theJsonMode.STRICT
output mode, and otherwise the default settings ofJsonWriterSettings.Builder
andDocumentCodec
.- Returns:
- a JSON representation of this document
- Throws:
CodecConfigurationException
- if the document contains types not in the default registry- See Also:
toJson(JsonWriterSettings)
,JsonWriterSettings
-
toJson
public java.lang.String toJson(JsonWriterSettings writerSettings)
Gets a JSON representation of this documentWith 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 java.lang.String toJson(Encoder<Document> encoder)
Gets a JSON representation of this documentWith 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 java.lang.String toJson(JsonWriterSettings writerSettings, Encoder<Document> encoder)
Gets a JSON representation of this document- Parameters:
writerSettings
- the json writer settings to use when encodingencoder
- 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:
size
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValue
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
containsKey
public boolean containsKey(java.lang.Object key)
- Specified by:
containsKey
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
get
public java.lang.Object get(java.lang.Object key)
- Specified by:
get
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
put
public java.lang.Object put(java.lang.String key, java.lang.Object value)
- Specified by:
put
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
remove
public java.lang.Object remove(java.lang.Object key)
- Specified by:
remove
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
putAll
public void putAll(java.util.Map<? extends java.lang.String,?> map)
- Specified by:
putAll
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
keySet
public java.util.Set<java.lang.String> keySet()
- Specified by:
keySet
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
values
public java.util.Collection<java.lang.Object> values()
- Specified by:
values
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
entrySet
public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet()
- Specified by:
entrySet
in interfacejava.util.Map<java.lang.String,java.lang.Object>
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equals
in interfacejava.util.Map<java.lang.String,java.lang.Object>
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfacejava.util.Map<java.lang.String,java.lang.Object>
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-