Package org.bson
Class BsonDocument
java.lang.Object
org.bson.BsonValue
org.bson.BsonDocument
- Direct Known Subclasses:
BsonDocumentWrapper,RawBsonDocument
public class BsonDocument
extends BsonValue
implements Map<String,BsonValue>, Cloneable, Bson, Serializable
A type-safe container for a BSON document. This class should NOT be sub-classed by third parties.
- Since:
- 3.0
- See Also:
-
Nested Class Summary
-
Field Summary
Fields inherited from interface org.bson.conversions.Bson
DEFAULT_CODEC_REGISTRY -
Constructor Summary
ConstructorsConstructorDescriptionConstruct an empty document.BsonDocument(int initialCapacity) Construct an empty document with the specified initial capacity.BsonDocument(String key, BsonValue value) Construct a new instance with a single key value pairBsonDocument(List<BsonElement> bsonElements) Construct a new instance with the given listBsonElement, none of which may be null. -
Method Summary
Modifier and TypeMethodDescriptionPut the given key and value into this document, and return the document.Gets the first value in the documentvoidclear()clone()booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleanIf the document does not contain the given key, return the given default value.Gets the value of the key if it is a BsonArray, or throws if not.If the document does not contain the given key, return the given default value.Gets the value of the key if it is a BsonBinary, or throws if not.getBinary(Object key, BsonBinary defaultValue) If the document does not contain the given key, return the given default value.getBoolean(Object key) Gets the value of the key if it is a BsonBoolean, or throws if not.getBoolean(Object key, BsonBoolean defaultValue) If the document does not contain the given key, return the given default value.Gets the BSON type of this value.getDateTime(Object key) Gets the value of the key if it is a BsonDateTime, or throws if not.getDateTime(Object key, BsonDateTime defaultValue) If the document does not contain the given key, return the given default value.getDecimal128(Object key) Gets the value of the key if it is a BsonDecimal128, or throws if not.getDecimal128(Object key, BsonDecimal128 defaultValue) If the document does not contain the given key, return the given default value.getDocument(Object key) Gets the value of the key if it is a BsonDocument, or throws if not.getDocument(Object key, BsonDocument defaultValue) If the document does not contain the given key, return the given default value.Gets the value of the key if it is a BsonDouble, or throws if not.getDouble(Object key, BsonDouble defaultValue) If the document does not contain the given key, return the given default value.Gets the first key in the document.Gets the value of the key if it is a BsonInt32, or throws if not.If the document does not contain the given key, return the given default value.Gets the value of the key if it is a BsonInt64, or throws if not.If the document does not contain the given key, return the given default value.Gets the value of the key if it is a BsonNumber, or throws if not.getNumber(Object key, BsonNumber defaultValue) If the document does not contain the given key, return the given default value.getObjectId(Object key) Gets the value of the key if it is a BsonObjectId, or throws if not.getObjectId(Object key, BsonObjectId defaultValue) If the document does not contain the given key, return the given default value.Gets the value of the key if it is a BsonRegularExpression, or throws if not.getRegularExpression(Object key, BsonRegularExpression defaultValue) If the document does not contain the given key, return the given default value.Gets the value of the key if it is a BsonString, or throws if not.getString(Object key, BsonString defaultValue) If the document does not contain the given key, return the given default value.getTimestamp(Object key) Gets the value of the key if it is a BsonTimestamp, or throws if not.getTimestamp(Object key, BsonTimestamp defaultValue) If the document does not contain the given key, return the given default value.inthashCode()booleanReturns true if the value of the key is a BsonArray, returns false if the document does not contain the key.booleanReturns true if the value of the key is a BsonBinary, returns false if the document does not contain the key.booleanReturns true if the value of the key is a BsonBoolean, returns false if the document does not contain the key.booleanisDateTime(Object key) Returns true if the value of the key is a BsonDateTime, returns false if the document does not contain the key.booleanisDecimal128(Object key) Returns true if the value of the key is a BsonDecimal128, returns false if the document does not contain the key.booleanisDocument(Object key) Returns true if the value of the key is a BsonDocument, returns false if the document does not contain the key.booleanReturns true if the value of the key is a BsonDouble, returns false if the document does not contain the key.booleanisEmpty()booleanReturns true if the value of the key is a BsonInt32, returns false if the document does not contain the key.booleanReturns true if the value of the key is a BsonInt64, returns false if the document does not contain the key.booleanReturns true if the value of the key is a BsonNull, returns false if the document does not contain the key.booleanReturns true if the value of the key is a BsonNumber, returns false if the document does not contain the key.booleanisObjectId(Object key) Returns true if the value of the key is a BsonObjectId, returns false if the document does not contain the key.booleanReturns true if the value of the key is a BsonString, returns false if the document does not contain the key.booleanisTimestamp(Object key) Returns true if the value of the key is a BsonTimestamp, returns false if the document does not contain the key.keySet()static BsonDocumentParses a string in MongoDB Extended JSON format to aBsonDocumentvoidintsize()<C> BsonDocumenttoBsonDocument(Class<C> documentClass, CodecRegistry codecRegistry) Render into a BsonDocument.toJson()Gets a JSON representation of this document using theJsonMode.RELAXEDoutput mode, and otherwise the default settings ofJsonWriterSettings.Builder.toJson(JsonWriterSettings settings) Gets a JSON representation of this document using the givenJsonWriterSettings.toString()values()Methods inherited from class org.bson.BsonValue
asArray, asBinary, asBoolean, asDateTime, asDBPointer, asDecimal128, asDocument, asDouble, asInt32, asInt64, asJavaScript, asJavaScriptWithScope, asNumber, asObjectId, asRegularExpression, asString, asSymbol, asTimestamp, isArray, isBinary, isBoolean, isDateTime, isDBPointer, isDecimal128, isDocument, isDouble, isInt32, isInt64, isJavaScript, isJavaScriptWithScope, isNull, isNumber, isObjectId, isRegularExpression, isString, isSymbol, isTimestampMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.bson.conversions.Bson
toBsonDocumentMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
BsonDocument
Construct a new instance with the given listBsonElement, none of which may be null.- Parameters:
bsonElements- a list ofBsonElement
-
BsonDocument
Construct a new instance with a single key value pair- Parameters:
key- the keyvalue- the value
-
BsonDocument
public BsonDocument(int initialCapacity) Construct an empty document with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity- Throws:
IllegalArgumentException- if the initial capacity is negative- Since:
- 4.3
-
BsonDocument
public BsonDocument()Construct an empty document.
-
-
Method Details
-
parse
Parses a string in MongoDB Extended JSON format to aBsonDocument- Parameters:
json- the JSON string- Returns:
- a corresponding
BsonDocumentobject - See Also:
- MongoDB documentation
- MongoDB Extended JSON
-
toBsonDocument
Description copied from interface:BsonRender into a BsonDocument.- Specified by:
toBsonDocumentin 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 returnedBsonDocumentbased on some knowledge of the document class.codecRegistry- the codec registry. This parameter may be ignored, but it may be used to look upCodecinstances for the document class or any other related class.- Returns:
- the BsonDocument
-
getBsonType
Description copied from class:BsonValueGets the BSON type of this value.- Specified by:
getBsonTypein classBsonValue- Returns:
- the BSON type, which may not be null (but may be BSONType.NULL)
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKeyin interfaceMap<String,BsonValue>
-
containsValue
- Specified by:
containsValuein interfaceMap<String,BsonValue>
-
get
-
getDocument
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
If the document does not contain the given key, return the given default value. Otherwise, gets the value of the key.- Parameters:
key- the keydefaultValue- the default value- Returns:
- the value of the key as a BsonValue
-
getDocument
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
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 keydefaultValue- 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
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
append
Put the given key and value into this document, and return the document.- Parameters:
key- the keyvalue- the value- Returns:
- this
-
getFirstKey
Gets the first key in the document.- Returns:
- the first key in the document
- Throws:
NoSuchElementException- if the document is empty- Since:
- 3.6
-
asBsonReader
Gets the first value in the document- Returns:
- the first value in the document
- Throws:
NoSuchElementException- if the document is empty- Since:
- 3.7
-
equals
-
hashCode
public int hashCode() -
toJson
Gets a JSON representation of this document using theJsonMode.RELAXEDoutput mode, and otherwise the default settings ofJsonWriterSettings.Builder.- Returns:
- a JSON representation of this document
- See Also:
-
toJson
Gets a JSON representation of this document using the givenJsonWriterSettings.- Parameters:
settings- the JSON writer settings- Returns:
- a JSON representation of this document
-
toString
-
clone
-