public class Document extends Object implements Map<String,Object>, 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(Map<String,Object> map)
Creates a Document instance initialized with the given map.
|
Document(String key,
Object value)
Create a Document instance initialized with the given key/value pair.
|
Modifier and Type | Method and Description |
---|---|
Document |
append(String key,
Object value)
Put the given key/value pair into this Document and return this.
|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<String,Object>> |
entrySet() |
boolean |
equals(Object o) |
Object |
get(Object key) |
<T> T |
get(Object key,
Class<T> clazz)
Gets the value of the given key, casting it to the given
Class<T> . |
<T> T |
get(Object key,
T defaultValue)
Gets the value of the given key, casting it to
Class<T> or returning the default value if null. |
Boolean |
getBoolean(Object key)
Gets the value of the given key as a Boolean.
|
boolean |
getBoolean(Object key,
boolean defaultValue)
Gets the value of the given key as a primitive boolean.
|
Date |
getDate(Object key)
Gets the value of the given key as a Date.
|
Double |
getDouble(Object key)
Gets the value of the given key as a Double.
|
Integer |
getInteger(Object key)
Gets the value of the given key as an Integer.
|
int |
getInteger(Object key,
int defaultValue)
Gets the value of the given key as a primitive int.
|
Long |
getLong(Object key)
Gets the value of the given key as a Long.
|
ObjectId |
getObjectId(Object key)
Gets the value of the given key as an ObjectId.
|
String |
getString(Object key)
Gets the value of the given key as a String.
|
int |
hashCode() |
boolean |
isEmpty() |
Set<String> |
keySet() |
static Document |
parse(String json)
Parses a string in MongoDB Extended JSON format to a
Document |
static Document |
parse(String json,
Decoder<Document> decoder)
Parses a string in MongoDB Extended JSON format to a
Document |
Object |
put(String key,
Object value) |
void |
putAll(Map<? extends String,?> map) |
Object |
remove(Object key) |
int |
size() |
<C> BsonDocument |
toBsonDocument(Class<C> documentClass,
CodecRegistry codecRegistry)
Render the filter into a BsonDocument.
|
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 . |
String |
toJson(Encoder<Document> encoder)
Gets a JSON representation of this document
|
String |
toJson(JsonWriterSettings writerSettings)
Gets a JSON representation of this document
|
String |
toJson(JsonWriterSettings writerSettings,
Encoder<Document> encoder)
Gets a JSON representation of this document
|
String |
toString() |
Collection<Object> |
values() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public Document()
public Document(String key, Object value)
key
- keyvalue
- valuepublic static Document parse(String json)
Document
json
- the JSON stringDocument
objectJsonReader
public static Document parse(String json, Decoder<Document> decoder)
Document
json
- the JSON stringdecoder
- the Decoder
to use to parse the JSON string into a Document
Document
objectJsonReader
public <C> BsonDocument toBsonDocument(Class<C> documentClass, CodecRegistry codecRegistry)
Bson
toBsonDocument
in interface Bson
C
- 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(String key, Object value)
doc.append("a", 1).append("b", 2)}
key
- keyvalue
- valuepublic <T> T get(Object key, 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 toClassCastException
- if the value of the given key is not of type Tpublic <T> T get(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 nullClassCastException
- if the value of the given key is not of type Tpublic Integer getInteger(Object key)
key
- the keyClassCastException
- if the value is not an integerpublic int getInteger(Object key, int defaultValue)
key
- the keydefaultValue
- what to return if the value is nullClassCastException
- if the value is not an integerpublic Long getLong(Object key)
key
- the keyClassCastException
- if the value is not an longpublic Double getDouble(Object key)
key
- the keyClassCastException
- if the value is not an doublepublic String getString(Object key)
key
- the keyClassCastException
- if the value is not a Stringpublic Boolean getBoolean(Object key)
key
- the keyClassCastException
- if the value is not an booleanpublic boolean getBoolean(Object key, boolean defaultValue)
key
- the keydefaultValue
- what to return if the value is nullClassCastException
- if the value is not a booleanpublic ObjectId getObjectId(Object key)
key
- the keyClassCastException
- if the value is not an ObjectIdpublic Date getDate(Object key)
key
- the keyClassCastException
- if the value is not a Datepublic 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)
,
JsonWriterSettings
public 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 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 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 boolean containsValue(Object value)
containsValue
in interface Map<String,Object>
public boolean containsKey(Object key)
containsKey
in interface Map<String,Object>
public boolean equals(Object o)
public int hashCode()