Package org.bson.codecs
Interface CollectibleCodec<T>
- Type Parameters:
T
- the document type
- All Known Implementing Classes:
BsonDocumentCodec
,DocumentCodec
A Codec that generates complete BSON documents for storage in a MongoDB collection.
- Since:
- 3.0
-
Method Summary
Modifier and TypeMethodDescriptionboolean
documentHasId
(T document) Returns true if the given document has an _id.generateIdIfAbsentFromDocument
(T document) Generates a value for the _id field on the given document, if the document does not have one.getDocumentId
(T document) Gets the _id of the given document if it contains one, otherwise throwsIllegalArgumentException
.Methods inherited from interface org.bson.codecs.Encoder
encode, getEncoderClass
-
Method Details
-
generateIdIfAbsentFromDocument
Generates a value for the _id field on the given document, if the document does not have one.- Parameters:
document
- the document for which to generate a value for the _id.- Returns:
- the document with the _id
-
documentHasId
Returns true if the given document has an _id.- Parameters:
document
- the document in which to look for an _id- Returns:
- true if the document has an _id
-
getDocumentId
Gets the _id of the given document if it contains one, otherwise throwsIllegalArgumentException
. To avoid the latter case, calldocumentHasId
first to check.- Parameters:
document
- the document from which to get the _id- Returns:
- the _id of the document
- Throws:
IllegalStateException
- if the document does not contain an _id
-