Class JacksonMongoCollection<TResult>
- java.lang.Object
-
- org.mongojack.MongoCollectionDecorator<TResult>
-
- org.mongojack.JacksonMongoCollection<TResult>
-
- All Implemented Interfaces:
com.mongodb.client.MongoCollection<TResult>
public class JacksonMongoCollection<TResult> extends MongoCollectionDecorator<TResult>
A DBCollection that marshals/demarshals objects to/from Jackson annotated classes. It provides a very thin wrapper over an existing MongoCollection.A JacksonMongoCollection is threadsafe, with a few caveats:
If you pass your own ObjectMapper to it, it is not thread safe to reconfigure that ObjectMapper at all after creating it. The setWritePreference and a few other methods on JacksonMongoCollection should not be called from multiple threads
Obtain an instance using JacksonMongoCollection.builder()...build()
Many of these methods accept queries or update documents in Bson format. You can assemble the Bson any way you want, including using Document:
new Document("foo", new Document("$gt", 7))
or using the mongo model builders for these objects:
Filters.eq("foo", 7)) Updates.inc("bar", 3)
- Since:
- 1.0
- Author:
- James Roper
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JacksonMongoCollection.JacksonMongoCollectionBuilder
Builder to buildJacksonMongoCollection
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static JacksonMongoCollection.JacksonMongoCollectionBuilder
builder()
Creates builder to build JacksonMongoCollection.org.bson.conversions.Bson
createIdInQuery(List<?> allIds)
org.bson.conversions.Bson
createIdQuery(Object id, Object... ids)
Creates a document query object for the _id field using the object as the _id.String
createSearchIndex(String indexName, org.bson.conversions.Bson definition)
String
createSearchIndex(org.bson.conversions.Bson definition)
List<String>
createSearchIndexes(List<com.mongodb.client.model.SearchIndexModel> searchIndexModels)
void
drop(com.mongodb.client.ClientSession clientSession, com.mongodb.client.model.DropCollectionOptions dropCollectionOptions)
void
drop(com.mongodb.client.model.DropCollectionOptions dropCollectionOptions)
void
dropSearchIndex(String indexName)
TResult
findOne()
Returns a single object from this collection.TResult
findOne(org.bson.conversions.Bson query)
Returns a single object from this collection matching the query.TResult
findOne(org.bson.conversions.Bson query, org.bson.conversions.Bson projection)
Returns a single object from this collection matching the query.TResult
findOneById(Object id)
Find an object by the given idJacksonCollectionKey<TResult>
getCollectionKey()
Get the type of this collectionString
getDatabaseName()
Gets the DB name in which the underlying collection is storedcom.mongodb.client.MongoCollection<TResult>
getMongoCollection()
Get the underlying mongo collectionString
getName()
Gets the name of the underlying collectionSerializationOptions
getSerializationOptions()
Class<TResult>
getValueClass()
void
insert(com.mongodb.WriteConcern concern, TResult... objects)
Inserts objects into the database.void
insert(List<TResult> list)
Inserts objects into the database.void
insert(List<TResult> list, com.mongodb.WriteConcern concern)
Inserts objects into the database.void
insert(TResult... objects)
Inserts objects into the database.com.mongodb.client.ListSearchIndexesIterable<org.bson.Document>
listSearchIndexes()
<TResult1> com.mongodb.client.ListSearchIndexesIterable<TResult1>
listSearchIndexes(Class<TResult1> tResult1Class)
protected List<org.bson.conversions.Bson>
manageAggregationPipeline(List<? extends org.bson.conversions.Bson> pipeline)
Does no real conversion, but it does initialize the pipeline correctly if it is one of the deprecated mongojack ones..protected List<com.mongodb.client.model.WriteModel<TResult>>
manageBulkWriteRequests(List<? extends com.mongodb.client.model.WriteModel<? extends TResult>> requests)
Manages the input write bson for a bulk write.protected org.bson.conversions.Bson
manageFilterBson(org.bson.conversions.Bson filter)
Serialize the fields of the given object using the object mapper for this collection.protected org.bson.conversions.Bson
manageUpdateBson(org.bson.conversions.Bson update)
Serialize the fields of the given object using the object mapper for this collection.protected List<org.bson.conversions.Bson>
manageUpdatePipeline(List<? extends org.bson.conversions.Bson> update)
Does a simple conversion (using toBsonDocument with this collection's CodecRegistry).protected com.mongodb.client.MongoCollection<TResult>
mongoCollection()
Get the mongo collection that we're operating on.com.mongodb.client.result.DeleteResult
removeById(Object _id)
Removes object from the database collection with the default WriteConcerncom.mongodb.client.result.UpdateResult
replaceOneById(Object _id, TResult object)
Performs an update operation, replacing the entire document, for the document with this _id.com.mongodb.client.result.UpdateResult
save(TResult object)
Saves and object to this collection (does insert or update based on the object _id).com.mongodb.client.result.UpdateResult
save(TResult object, com.mongodb.WriteConcern concern)
Saves an object to this collection (does insert or update based on the object _id).String
toString()
com.mongodb.client.result.UpdateResult
updateById(Object _id, org.bson.conversions.Bson update)
Performs an update operation.void
updateSearchIndex(String indexName, org.bson.conversions.Bson definition)
com.mongodb.client.MongoCollection<TResult>
withCodecRegistry(org.bson.codecs.configuration.CodecRegistry codecRegistry)
<NewTDocument>
JacksonMongoCollection<NewTDocument>withDocumentClass(Class<NewTDocument> clazz)
JacksonMongoCollection<TResult>
withReadConcern(com.mongodb.ReadConcern readConcern)
JacksonMongoCollection<TResult>
withReadPreference(com.mongodb.ReadPreference readPreference)
JacksonMongoCollection<TResult>
withWriteConcern(com.mongodb.WriteConcern writeConcern)
protected <T1> com.mongodb.client.DistinctIterable<T1>
wrapIterable(com.mongodb.client.DistinctIterable<T1> input)
Wraps an iterable that supports further .filter() calls so that we can perform mapping on them.protected <T1> com.mongodb.client.FindIterable<T1>
wrapIterable(com.mongodb.client.FindIterable<T1> input)
Wraps an iterable that supports further .filter() calls so that we can perform mapping on them.protected <T1> com.mongodb.client.MapReduceIterable<T1>
wrapIterable(com.mongodb.client.MapReduceIterable<T1> input)
Wraps an iterable that supports further .filter() calls so that we can perform mapping on them.-
Methods inherited from class org.mongojack.MongoCollectionDecorator
aggregate, aggregate, aggregate, aggregate, bulkWrite, bulkWrite, bulkWrite, bulkWrite, countDocuments, countDocuments, countDocuments, countDocuments, countDocuments, countDocuments, createIndex, createIndex, createIndex, createIndex, createIndexes, createIndexes, createIndexes, createIndexes, deleteMany, deleteMany, deleteMany, deleteMany, deleteOne, deleteOne, deleteOne, deleteOne, distinct, distinct, distinct, distinct, drop, drop, dropIndex, dropIndex, dropIndex, dropIndex, dropIndex, dropIndex, dropIndex, dropIndex, dropIndexes, dropIndexes, dropIndexes, dropIndexes, estimatedDocumentCount, estimatedDocumentCount, find, find, find, find, find, find, find, find, findOneAndDelete, findOneAndDelete, findOneAndDelete, findOneAndDelete, findOneAndReplace, findOneAndReplace, findOneAndReplace, findOneAndReplace, findOneAndUpdate, findOneAndUpdate, findOneAndUpdate, findOneAndUpdate, findOneAndUpdate, findOneAndUpdate, findOneAndUpdate, findOneAndUpdate, getCodecRegistry, getDocumentClass, getNamespace, getReadConcern, getReadPreference, getWriteConcern, insertMany, insertMany, insertMany, insertMany, insertOne, insertOne, insertOne, insertOne, listIndexes, listIndexes, listIndexes, listIndexes, mapReduce, mapReduce, mapReduce, mapReduce, renameCollection, renameCollection, renameCollection, renameCollection, replaceOne, replaceOne, replaceOne, replaceOne, updateMany, updateMany, updateMany, updateMany, updateMany, updateMany, updateMany, updateMany, updateOne, updateOne, updateOne, updateOne, updateOne, updateOne, updateOne, updateOne, watch, watch, watch, watch, watch, watch, watch, watch
-
-
-
-
Method Detail
-
builder
public static JacksonMongoCollection.JacksonMongoCollectionBuilder builder()
Creates builder to build JacksonMongoCollection.- Returns:
- created builder
-
findOne
public TResult findOne() throws com.mongodb.MongoException
Returns a single object from this collection.- Returns:
- the object found, or
null
if the collection is empty - Throws:
com.mongodb.MongoException
- If an error occurred
-
findOne
public TResult findOne(org.bson.conversions.Bson query)
Returns a single object from this collection matching the query.- Parameters:
query
- the query object- Returns:
- the object found, or
null
if no such object exists
-
findOne
public TResult findOne(org.bson.conversions.Bson query, org.bson.conversions.Bson projection)
Returns a single object from this collection matching the query.- Parameters:
query
- the query objectprojection
- the projection- Returns:
- the object found, or
null
if no such object exists
-
findOneById
public TResult findOneById(Object id) throws com.mongodb.MongoException
Find an object by the given id- Parameters:
id
- The id- Returns:
- The object
- Throws:
com.mongodb.MongoException
- If an error occurred
-
createIdQuery
public org.bson.conversions.Bson createIdQuery(Object id, Object... ids)
Creates a document query object for the _id field using the object as the _id. This object is expected to already be in the correct format... Document, Long, String, etc...- Parameters:
id
- An id to search forids
- Other ids to search for- Returns:
- A Bson query for the id or ids
-
createIdInQuery
public org.bson.conversions.Bson createIdInQuery(List<?> allIds)
-
getCollectionKey
public JacksonCollectionKey<TResult> getCollectionKey()
Get the type of this collection- Returns:
- The type
-
getMongoCollection
public com.mongodb.client.MongoCollection<TResult> getMongoCollection()
Get the underlying mongo collection- Returns:
- The underlying mongo collection
-
getDatabaseName
public String getDatabaseName()
Gets the DB name in which the underlying collection is stored- Returns:
- The name of the database in which this collection is being stored.
-
getName
public String getName()
Gets the name of the underlying collection- Returns:
- the name of the collection
-
insert
@SafeVarargs public final void insert(TResult... objects) throws com.mongodb.MongoException, com.mongodb.MongoBulkWriteException
Inserts objects into the database. if the objects' _id are null, they will be generated.- Parameters:
objects
- The objects to insert- Throws:
com.mongodb.MongoBulkWriteException
- If there's an exception in the bulk write operationcom.mongodb.MongoException
- If an error occurred
-
insert
public void insert(List<TResult> list) throws com.mongodb.MongoException, com.mongodb.MongoBulkWriteException
Inserts objects into the database. if the objects' _id are null, they will be generated.- Parameters:
list
- The objects to insert- Throws:
com.mongodb.MongoBulkWriteException
- If there's an exception in the bulk write operationcom.mongodb.MongoException
- If an error occurred
-
insert
@SafeVarargs public final void insert(com.mongodb.WriteConcern concern, TResult... objects) throws com.mongodb.MongoException, com.mongodb.MongoBulkWriteException
Inserts objects into the database. if the objects' _id are null, they will be generated.- Parameters:
objects
- The objects to insertconcern
- the write concern- Throws:
com.mongodb.MongoBulkWriteException
- If there's an exception in the bulk write operationcom.mongodb.MongoException
- If an error occurred
-
insert
public void insert(List<TResult> list, com.mongodb.WriteConcern concern) throws com.mongodb.MongoException
Inserts objects into the database. if the objects' _id are null, they will be generated.- Parameters:
list
- The objects to insertconcern
- the write concern- Throws:
com.mongodb.MongoBulkWriteException
- If there's an exception in the bulk write operationcom.mongodb.MongoException
- If an error occurred
-
removeById
public com.mongodb.client.result.DeleteResult removeById(Object _id) throws com.mongodb.MongoException, com.mongodb.MongoWriteException, com.mongodb.MongoWriteConcernException
Removes object from the database collection with the default WriteConcern- Parameters:
_id
- the id of the document to remove- Returns:
- The delete result
- Throws:
com.mongodb.MongoWriteException
- If the write failed due some other failure specific to the delete commandcom.mongodb.MongoWriteConcernException
- If the write failed due being unable to fulfill the write concerncom.mongodb.MongoException
- If an error occurred
-
replaceOneById
public com.mongodb.client.result.UpdateResult replaceOneById(Object _id, TResult object) throws com.mongodb.MongoException, com.mongodb.MongoWriteException, com.mongodb.MongoWriteConcernException
Performs an update operation, replacing the entire document, for the document with this _id.- Parameters:
_id
- the _id of the object to replaceobject
- object with which to replace it- Returns:
- The result
- Throws:
com.mongodb.MongoWriteException
- If the write failed due some other failure specific to the update commandcom.mongodb.MongoWriteConcernException
- If the write failed due being unable to fulfill the write concerncom.mongodb.MongoException
- If an error occurred
-
save
public com.mongodb.client.result.UpdateResult save(TResult object) throws com.mongodb.MongoWriteException, com.mongodb.MongoWriteConcernException, com.mongodb.MongoException
Saves and object to this collection (does insert or update based on the object _id). Uses default write concern.- Parameters:
object
- the object to save. will add_id
field to object if needed- Returns:
- The UpdateResult result
- Throws:
com.mongodb.MongoWriteException
- If the write failed due some other failure specific to the delete commandcom.mongodb.MongoWriteConcernException
- If the write failed due being unable to fulfill the write concerncom.mongodb.MongoException
- If an error occurred
-
save
public com.mongodb.client.result.UpdateResult save(TResult object, com.mongodb.WriteConcern concern) throws com.mongodb.MongoWriteException, com.mongodb.MongoWriteConcernException, com.mongodb.MongoException
Saves an object to this collection (does insert or update based on the object _id).- Parameters:
object
- theDBObject
to saveconcern
- the write concern- Returns:
- The UpdateResult result
- Throws:
com.mongodb.MongoWriteException
- If the write failed due some other failure specific to the delete commandcom.mongodb.MongoWriteConcernException
- If the write failed due being unable to fulfill the write concerncom.mongodb.MongoException
- If an error occurred
-
getSerializationOptions
public SerializationOptions getSerializationOptions()
-
mongoCollection
protected com.mongodb.client.MongoCollection<TResult> mongoCollection()
Description copied from class:MongoCollectionDecorator
Get the mongo collection that we're operating on.- Specified by:
mongoCollection
in classMongoCollectionDecorator<TResult>
- Returns:
- A mongo collection. Must not be null. Must not be this.
-
manageUpdateBson
protected org.bson.conversions.Bson manageUpdateBson(org.bson.conversions.Bson update)
Serialize the fields of the given object using the object mapper for this collection. This will convert POJOs to DBObjects where necessary.Manage the input Bson in any way necessary to produce a proper update. The expectation is that this is passed through the mapper or the codec, but the implementation is free to do what it wants as long as the returned Bson is valid.
- Specified by:
manageUpdateBson
in classMongoCollectionDecorator<TResult>
- Parameters:
update
- a valid Bson update document (e.g. containing $set, etc).- Returns:
- a valid Bson update document (e.g. containing $set, etc) transformed as necessary
-
manageUpdatePipeline
protected List<org.bson.conversions.Bson> manageUpdatePipeline(List<? extends org.bson.conversions.Bson> update)
Does a simple conversion (using toBsonDocument with this collection's CodecRegistry).Manage the input Bson in any way necessary to produce a proper update. The expectation is that this is passed through the mapper or the codec, but the implementation is free to do what it wants as long as the returned Bson is valid.
- Specified by:
manageUpdatePipeline
in classMongoCollectionDecorator<TResult>
- Parameters:
update
- a valid Bson update pipeline (e.g. containing $set, etc).- Returns:
- a valid Bson update pipeline (e.g. containing $set, etc) transformed as necessary
-
manageFilterBson
protected org.bson.conversions.Bson manageFilterBson(org.bson.conversions.Bson filter)
Serialize the fields of the given object using the object mapper for this collection. This will convert POJOs to DBObjects where necessary.Manage the input Bson in any way necessary to produce a proper filter. The expectation is that this is passed through the mapper or the codec, but the implementation is free to do what it wants as long as the returned Bson is valid.
- Specified by:
manageFilterBson
in classMongoCollectionDecorator<TResult>
- Parameters:
filter
- a valid Bson filter document.- Returns:
- a valid Bson filter document transformed as necessary
-
manageAggregationPipeline
protected List<org.bson.conversions.Bson> manageAggregationPipeline(List<? extends org.bson.conversions.Bson> pipeline)
Does no real conversion, but it does initialize the pipeline correctly if it is one of the deprecated mongojack ones..Manage the input Bson in any way necessary to produce a proper pipeline. The expectation is that this is passed through the mapper or the codec, but the implementation is free to do what it wants as long as the returned Bson is valid.
- Specified by:
manageAggregationPipeline
in classMongoCollectionDecorator<TResult>
- Parameters:
pipeline
- a list of Bson documents making up an aggregation pipeline- Returns:
- a list of Bson documents making up an aggregation pipeline, transformed as necessary
-
manageBulkWriteRequests
protected List<com.mongodb.client.model.WriteModel<TResult>> manageBulkWriteRequests(List<? extends com.mongodb.client.model.WriteModel<? extends TResult>> requests)
Description copied from class:MongoCollectionDecorator
Manages the input write bson for a bulk write.- Specified by:
manageBulkWriteRequests
in classMongoCollectionDecorator<TResult>
- Parameters:
requests
- A list of WriteModel instances- Returns:
- A list of WriteModel instances with filter and update BSON documents updated with the mapper
-
wrapIterable
protected <T1> com.mongodb.client.DistinctIterable<T1> wrapIterable(com.mongodb.client.DistinctIterable<T1> input)
Description copied from class:MongoCollectionDecorator
Wraps an iterable that supports further .filter() calls so that we can perform mapping on them.- Specified by:
wrapIterable
in classMongoCollectionDecorator<TResult>
- Type Parameters:
T1
- return type of the iterable- Parameters:
input
- a valid iterable- Returns:
- a wrapped iterable that supports some extended filter operations
-
wrapIterable
protected <T1> com.mongodb.client.FindIterable<T1> wrapIterable(com.mongodb.client.FindIterable<T1> input)
Description copied from class:MongoCollectionDecorator
Wraps an iterable that supports further .filter() calls so that we can perform mapping on them.- Specified by:
wrapIterable
in classMongoCollectionDecorator<TResult>
- Type Parameters:
T1
- return type of the iterable- Parameters:
input
- a valid iterable- Returns:
- a wrapped iterable that supports some extended filter operations
-
wrapIterable
protected <T1> com.mongodb.client.MapReduceIterable<T1> wrapIterable(com.mongodb.client.MapReduceIterable<T1> input)
Description copied from class:MongoCollectionDecorator
Wraps an iterable that supports further .filter() calls so that we can perform mapping on them.- Specified by:
wrapIterable
in classMongoCollectionDecorator<TResult>
- Type Parameters:
T1
- return type of the iterable- Parameters:
input
- a valid iterable- Returns:
- a wrapped iterable that supports some extended filter operations
-
updateById
public com.mongodb.client.result.UpdateResult updateById(Object _id, org.bson.conversions.Bson update) throws com.mongodb.MongoException, com.mongodb.MongoWriteException, com.mongodb.MongoWriteConcernException
Performs an update operation.- Parameters:
_id
- The id of the document to updateupdate
- update with which to update- Returns:
- The write result
- Throws:
com.mongodb.MongoWriteException
- If the write failed due some other failure specific to the update commandcom.mongodb.MongoWriteConcernException
- If the write failed due being unable to fulfill the write concerncom.mongodb.MongoException
- If an error occurred
-
withDocumentClass
public <NewTDocument> JacksonMongoCollection<NewTDocument> withDocumentClass(Class<NewTDocument> clazz)
-
withCodecRegistry
public com.mongodb.client.MongoCollection<TResult> withCodecRegistry(org.bson.codecs.configuration.CodecRegistry codecRegistry)
-
withReadPreference
public JacksonMongoCollection<TResult> withReadPreference(com.mongodb.ReadPreference readPreference)
-
withWriteConcern
public JacksonMongoCollection<TResult> withWriteConcern(com.mongodb.WriteConcern writeConcern)
-
withReadConcern
public JacksonMongoCollection<TResult> withReadConcern(com.mongodb.ReadConcern readConcern)
-
drop
public void drop(com.mongodb.client.model.DropCollectionOptions dropCollectionOptions)
-
drop
public void drop(com.mongodb.client.ClientSession clientSession, com.mongodb.client.model.DropCollectionOptions dropCollectionOptions)
-
createSearchIndex
public String createSearchIndex(String indexName, org.bson.conversions.Bson definition)
-
createSearchIndex
public String createSearchIndex(org.bson.conversions.Bson definition)
-
createSearchIndexes
public List<String> createSearchIndexes(List<com.mongodb.client.model.SearchIndexModel> searchIndexModels)
-
updateSearchIndex
public void updateSearchIndex(String indexName, org.bson.conversions.Bson definition)
-
dropSearchIndex
public void dropSearchIndex(String indexName)
-
listSearchIndexes
public com.mongodb.client.ListSearchIndexesIterable<org.bson.Document> listSearchIndexes()
-
listSearchIndexes
public <TResult1> com.mongodb.client.ListSearchIndexesIterable<TResult1> listSearchIndexes(Class<TResult1> tResult1Class)
-
-