Package org.bson
Class BsonDocument
-
- org.bson.BsonValue
-
- org.bson.BsonDocument
-
- Direct Known Subclasses:
BsonDocumentWrapper
,RawBsonDocument
public class BsonDocument extends BsonValue implements <,,
A type-safe container for a BSON document. This class should NOT be sub-classed by third parties.- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description BsonDocument()
Construct an empty document.key, BsonValue value)
Construct a new instance with a single key value pair<BsonElement> bsonElements)
Construct a new instance with the given listBsonElement
, none of which may be null.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BsonDocument
key, BsonValue value)
Put the given key and value into this document, and return the document.BsonReader
asBsonReader()
Gets the first value in the documentvoid
clear()
BsonDocument
clone()
boolean
key)
boolean
value)
<<,BsonValue>>
entrySet()
boolean
o)
BsonValue
key)
BsonValue
key, BsonValue defaultValue)
If the document does not contain the given key, return the given default value.BsonArray
key)
Gets the value of the key if it is a BsonArray, or throws if not.BsonArray
key, BsonArray defaultValue)
If the document does not contain the given key, return the given default value.BsonBinary
key)
Gets the value of the key if it is a BsonBinary, or throws if not.BsonBinary
key, BsonBinary defaultValue)
If the document does not contain the given key, return the given default value.BsonBoolean
key)
Gets the value of the key if it is a BsonBoolean, or throws if not.BsonBoolean
key, BsonBoolean defaultValue)
If the document does not contain the given key, return the given default value.BsonType
getBsonType()
Gets the BSON type of this value.BsonDateTime
key)
Gets the value of the key if it is a BsonDateTime, or throws if not.BsonDateTime
key, BsonDateTime defaultValue)
If the document does not contain the given key, return the given default value.BsonDecimal128
key)
Gets the value of the key if it is a BsonDecimal128, or throws if not.BsonDecimal128
key, BsonDecimal128 defaultValue)
If the document does not contain the given key, return the given default value.BsonDocument
key)
Gets the value of the key if it is a BsonDocument, or throws if not.BsonDocument
key, BsonDocument defaultValue)
If the document does not contain the given key, return the given default value.BsonDouble
key)
Gets the value of the key if it is a BsonDouble, or throws if not.BsonDouble
key, BsonDouble defaultValue)
If the document does not contain the given key, return the given default value.getFirstKey()
Gets the first key in the document.BsonInt32
key)
Gets the value of the key if it is a BsonInt32, or throws if not.BsonInt32
key, BsonInt32 defaultValue)
If the document does not contain the given key, return the given default value.BsonInt64
key)
Gets the value of the key if it is a BsonInt64, or throws if not.BsonInt64
key, BsonInt64 defaultValue)
If the document does not contain the given key, return the given default value.BsonNumber
key)
Gets the value of the key if it is a BsonNumber, or throws if not.BsonNumber
key, BsonNumber defaultValue)
If the document does not contain the given key, return the given default value.BsonObjectId
key)
Gets the value of the key if it is a BsonObjectId, or throws if not.BsonObjectId
key, BsonObjectId defaultValue)
If the document does not contain the given key, return the given default value.BsonRegularExpression
key)
Gets the value of the key if it is a BsonRegularExpression, or throws if not.BsonRegularExpression
key, BsonRegularExpression defaultValue)
If the document does not contain the given key, return the given default value.BsonString
key)
Gets the value of the key if it is a BsonString, or throws if not.BsonString
key, BsonString defaultValue)
If the document does not contain the given key, return the given default value.BsonTimestamp
key)
Gets the value of the key if it is a BsonTimestamp, or throws if not.BsonTimestamp
key, BsonTimestamp defaultValue)
If the document does not contain the given key, return the given default value.int
hashCode()
boolean
key)
Returns true if the value of the key is a BsonArray, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonBinary, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonBoolean, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonDateTime, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonDecimal128, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonDocument, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonDouble, returns false if the document does not contain the key.boolean
isEmpty()
boolean
key)
Returns true if the value of the key is a BsonInt32, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonInt64, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonNull, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonNumber, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonObjectId, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonString, returns false if the document does not contain the key.boolean
key)
Returns true if the value of the key is a BsonTimestamp, returns false if the document does not contain the key.<>
keySet()
static BsonDocument
json)
Parses a string in MongoDB Extended JSON format to aBsonDocument
BsonValue
key, BsonValue value)
void
<? extends ,? extends BsonValue> m)
BsonValue
key)
int
size()
<C> BsonDocument
<C> documentClass, CodecRegistry codecRegistry)
Render the filter into a BsonDocument.toJson()
Gets a JSON representation of this document using theJsonMode.STRICT
output mode, and otherwise the default settings ofJsonWriterSettings.Builder
.toJson(JsonWriterSettings settings)
Gets a JSON representation of this document using the givenJsonWriterSettings
.toString()
<BsonValue>
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, isTimestamp
-
-
-
-
Constructor Detail
-
BsonDocument
public BsonDocument(<BsonElement> bsonElements)
Construct a new instance with the given listBsonElement
, none of which may be null.- Parameters:
bsonElements
- a list ofBsonElement
-
BsonDocument
public BsonDocument( key, BsonValue value)
Construct a new instance with a single key value pair- Parameters:
key
- the keyvalue
- the value
-
BsonDocument
public BsonDocument()
Construct an empty document.
-
-
Method Detail
-
parse
public static json)
Parses a string in MongoDB Extended JSON format to aBsonDocument
- Parameters:
json
- the JSON string- Returns:
- a corresponding
BsonDocument
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 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
-
getBsonType
public BsonType getBsonType()
Description copied from class:BsonValue
Gets the BSON type of this value.- Specified by:
getBsonType
in classBsonValue
- Returns:
- the BSON type, which may not be null (but may be BSONType.NULL)
-
size
public int size()
- Specified by:
in interface
<,BsonValue>
-
isEmpty
public boolean isEmpty()
- Specified by:
in interface
<,BsonValue>
-
containsKey
public boolean containsKey( key)
- Specified by:
in interface
<,BsonValue>
-
containsValue
public boolean containsValue( value)
- Specified by:
in interface
<,BsonValue>
-
get
public key)
- Specified by:
in interface
<,BsonValue>
-
getDocument
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public key)
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
public boolean isNull( key)
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
public boolean isDocument( key)
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
public boolean isArray( key)
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
public boolean isNumber( key)
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
public boolean isInt32( key)
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
public boolean isInt64( key)
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
public boolean isDecimal128( key)
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
public boolean isDouble( key)
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
public boolean isBoolean( key)
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
public boolean isString( key)
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
public boolean isDateTime( key)
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
public boolean isTimestamp( key)
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
public boolean isObjectId( key)
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
public boolean isBinary( key)
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
public key, BsonValue defaultValue)
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
public key, BsonDocument defaultValue)
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
public key, BsonArray defaultValue)
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
public key, BsonNumber defaultValue)
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
public key, BsonInt32 defaultValue)
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
public key, BsonInt64 defaultValue)
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
public key, BsonDecimal128 defaultValue)
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
public key, BsonDouble defaultValue)
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
public key, BsonBoolean defaultValue)
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
public key, BsonString defaultValue)
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
public key, BsonDateTime defaultValue)
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
public key, BsonTimestamp defaultValue)
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
public key, BsonObjectId defaultValue)
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
public key, BsonBinary defaultValue)
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
public key, BsonRegularExpression defaultValue)
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
-
remove
public key)
- Specified by:
in interface
<,BsonValue>
-
putAll
public void putAll(<? extends ,? extends BsonValue> m)
- Specified by:
in interface
<,BsonValue>
-
clear
public void clear()
- Specified by:
in interface
<,BsonValue>
-
keySet
public <> keySet()
- Specified by:
in interface
<,BsonValue>
-
append
public key, BsonValue value)
Put the given key and value into this document, and return the document.- Parameters:
key
- the keyvalue
- the value- Returns:
- this
-
getFirstKey
public getFirstKey()
Gets the first key in the document.- Returns:
- the first key in the document
- Throws:
- if the document is empty
- Since:
- 3.6
-
asBsonReader
public BsonReader asBsonReader()
Gets the first value in the document- Returns:
- the first value in the document
- Throws:
- if the document is empty
- Since:
- 3.7
-
equals
public boolean equals( o)
- Specified by:
in interface
<,BsonValue>
- Overrides:
in class
-
hashCode
public int hashCode()
- Specified by:
in interface
<,BsonValue>
- Overrides:
in class
-
toJson
public toJson()
Gets a JSON representation of this document using theJsonMode.STRICT
output mode, and otherwise the default settings ofJsonWriterSettings.Builder
.- Returns:
- a JSON representation of this document
- See Also:
toJson(JsonWriterSettings)
,JsonWriterSettings
-
toJson
public toJson(JsonWriterSettings settings)
Gets a JSON representation of this document using the givenJsonWriterSettings
.- Parameters:
settings
- the JSON writer settings- Returns:
- a JSON representation of this document
-
toString
public toString()
- Overrides:
in class
-
clone
public BsonDocument clone()
- Overrides:
in class
-
-