public class Document extends java.lang.Object implements java.util.Map<java.lang.String,java.lang.Object>, java.io.Serializable, Bson
Map. All iterators will traverse the elements in insertion order, as with LinkedHashMap.| Constructor and Description |
|---|
Document()
Creates an empty Document instance.
|
Document(java.util.Map<java.lang.String,java.lang.Object> map)
Creates a Document instance initialized with the given map.
|
Document(java.lang.String key,
java.lang.Object value)
Create a Document instance initialized with the given key/value pair.
|
| Modifier and Type | Method and 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 given
Class<T>. |
<T> T |
get(java.lang.Object key,
T defaultValue)
Gets the value of the given key, casting it to
Class<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 given
Class<T>. |
<T> T |
getEmbedded(java.util.List<?> keys,
T defaultValue)
Gets the value in an embedded document, casting it to the given
Class<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 given
Class<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 to
Class<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 a
Document |
static Document |
parse(java.lang.String json,
Decoder<Document> decoder)
Parses a string in MongoDB Extended JSON format to a
Document |
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 the
JsonMode.STRICT output mode, and otherwise the default
settings of JsonWriterSettings.Builder and DocumentCodec. |
java.lang.String |
toJson(Encoder<Document> encoder)
Gets a JSON representation of this document
|
java.lang.String |
toJson(JsonWriterSettings writerSettings)
Gets a JSON representation of this document
|
java.lang.String |
toJson(JsonWriterSettings writerSettings,
Encoder<Document> encoder)
Gets a JSON representation of this document
|
java.lang.String |
toString() |
java.util.Collection<java.lang.Object> |
values() |
public Document()
public Document(java.lang.String key,
java.lang.Object value)
key - keyvalue - valuepublic Document(java.util.Map<java.lang.String,java.lang.Object> map)
map - initial mappublic static Document parse(java.lang.String json)
Documentjson - the JSON stringDocument objectJsonReaderpublic static Document parse(java.lang.String json, Decoder<Document> decoder)
Documentjson - the JSON stringdecoder - the Decoder to use to parse the JSON string into a DocumentDocument objectJsonReaderpublic <C> BsonDocument toBsonDocument(java.lang.Class<C> documentClass, CodecRegistry codecRegistry)
BsontoBsonDocument in interface BsonC - the type of the document classdocumentClass - 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.public Document append(java.lang.String key, java.lang.Object value)
doc.append("a", 1).append("b", 2)}
key - keyvalue - valuepublic <T> T get(java.lang.Object key,
java.lang.Class<T> clazz)
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") .T - the type of the classkey - the keyclazz - the non-null class to cast the value tojava.lang.ClassCastException - if the value of the given key is not of type Tpublic <T> T get(java.lang.Object key,
T defaultValue)
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.T - the type of the classkey - the keydefaultValue - what to return if the value is nulljava.lang.ClassCastException - if the value of the given key is not of type Tpublic <T> T getEmbedded(java.util.List<?> keys,
java.lang.Class<T> clazz)
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") .T - the type of the classkeys - the list of keysclazz - the non-null class to cast the value tojava.lang.ClassCastException - if the value of the given embedded key is not of type Tpublic <T> T getEmbedded(java.util.List<?> keys,
T defaultValue)
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") .T - the type of the classkeys - the list of keysdefaultValue - what to return if the value is nulljava.lang.ClassCastException - if the value of the given key is not of type Tpublic java.lang.Integer getInteger(java.lang.Object key)
key - the keyjava.lang.ClassCastException - if the value is not an integerpublic int getInteger(java.lang.Object key,
int defaultValue)
key - the keydefaultValue - what to return if the value is nulljava.lang.ClassCastException - if the value is not an integerpublic java.lang.Long getLong(java.lang.Object key)
key - the keyjava.lang.ClassCastException - if the value is not an longpublic java.lang.Double getDouble(java.lang.Object key)
key - the keyjava.lang.ClassCastException - if the value is not an doublepublic java.lang.String getString(java.lang.Object key)
key - the keyjava.lang.ClassCastException - if the value is not a Stringpublic java.lang.Boolean getBoolean(java.lang.Object key)
key - the keyjava.lang.ClassCastException - if the value is not an booleanpublic boolean getBoolean(java.lang.Object key,
boolean defaultValue)
key - the keydefaultValue - what to return if the value is nulljava.lang.ClassCastException - if the value is not a booleanpublic ObjectId getObjectId(java.lang.Object key)
key - the keyjava.lang.ClassCastException - if the value is not an ObjectIdpublic java.util.Date getDate(java.lang.Object key)
key - the keyjava.lang.ClassCastException - if the value is not a Datepublic <T> java.util.List<T> getList(java.lang.Object key,
java.lang.Class<T> clazz)
Class<T>. This is useful to avoid having
casts in client code, though the effect is the same.T - the type of the classkey - the keyclazz - the non-null class to cast the list value tojava.lang.ClassCastException - if the elements in the list value of the given key is not of type T or the value is not a listpublic <T> java.util.List<T> getList(java.lang.Object key,
java.lang.Class<T> clazz,
java.util.List<T> defaultValue)
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.T - the type of the classkey - the keyclazz - the non-null class to cast the list value todefaultValue - what to return if the value is nulljava.lang.ClassCastException - if the value of the given key is not of type Tpublic java.lang.String toJson()
JsonMode.STRICT output mode, and otherwise the default
settings of JsonWriterSettings.Builder and DocumentCodec.CodecConfigurationException - if the document contains types not in the default registrytoJson(JsonWriterSettings),
JsonWriterSettingspublic java.lang.String toJson(JsonWriterSettings writerSettings)
With the default DocumentCodec.
writerSettings - the json writer settings to use when encodingCodecConfigurationException - if the document contains types not in the default registrypublic java.lang.String toJson(Encoder<Document> encoder)
With the default JsonWriterSettings.
encoder - the document codec instance to use to encode the documentCodecConfigurationException - if the registry does not contain a codec for the document values.public java.lang.String toJson(JsonWriterSettings writerSettings, Encoder<Document> encoder)
writerSettings - the json writer settings to use when encodingencoder - the document codec instance to use to encode the documentCodecConfigurationException - if the registry does not contain a codec for the document values.public int size()
size in interface java.util.Map<java.lang.String,java.lang.Object>public boolean isEmpty()
isEmpty in interface java.util.Map<java.lang.String,java.lang.Object>public boolean containsValue(java.lang.Object value)
containsValue in interface java.util.Map<java.lang.String,java.lang.Object>public boolean containsKey(java.lang.Object key)
containsKey in interface java.util.Map<java.lang.String,java.lang.Object>public java.lang.Object get(java.lang.Object key)
get in interface java.util.Map<java.lang.String,java.lang.Object>public java.lang.Object put(java.lang.String key,
java.lang.Object value)
put in interface java.util.Map<java.lang.String,java.lang.Object>public java.lang.Object remove(java.lang.Object key)
remove in interface java.util.Map<java.lang.String,java.lang.Object>public void putAll(java.util.Map<? extends java.lang.String,?> map)
putAll in interface java.util.Map<java.lang.String,java.lang.Object>public void clear()
clear in interface java.util.Map<java.lang.String,java.lang.Object>public java.util.Set<java.lang.String> keySet()
keySet in interface java.util.Map<java.lang.String,java.lang.Object>public java.util.Collection<java.lang.Object> values()
values in interface java.util.Map<java.lang.String,java.lang.Object>public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet()
entrySet in interface java.util.Map<java.lang.String,java.lang.Object>public boolean equals(java.lang.Object o)
equals in interface java.util.Map<java.lang.String,java.lang.Object>equals in class java.lang.Objectpublic int hashCode()
hashCode in interface java.util.Map<java.lang.String,java.lang.Object>hashCode in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2010 - 2023 Adobe. All Rights Reserved