Package org.mongojack

Class 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
    • Method Detail

      • 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 object
        projection - 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 for
        ids - Other ids to search for
        Returns:
        A Bson query for the id or ids
      • createIdInQuery

        public org.bson.conversions.Bson createIdInQuery​(List<?> allIds)
      • 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 operation
        com.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 operation
        com.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 insert
        concern - the write concern
        Throws:
        com.mongodb.MongoBulkWriteException - If there's an exception in the bulk write operation
        com.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 insert
        concern - the write concern
        Throws:
        com.mongodb.MongoBulkWriteException - If there's an exception in the bulk write operation
        com.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 command
        com.mongodb.MongoWriteConcernException - If the write failed due being unable to fulfill the write concern
        com.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 replace
        object - 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 command
        com.mongodb.MongoWriteConcernException - If the write failed due being unable to fulfill the write concern
        com.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 command
        com.mongodb.MongoWriteConcernException - If the write failed due being unable to fulfill the write concern
        com.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 - the DBObject to save
        concern - the write concern
        Returns:
        The UpdateResult result
        Throws:
        com.mongodb.MongoWriteException - If the write failed due some other failure specific to the delete command
        com.mongodb.MongoWriteConcernException - If the write failed due being unable to fulfill the write concern
        com.mongodb.MongoException - If an error occurred
      • 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 class MongoCollectionDecorator<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 class MongoCollectionDecorator<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 class MongoCollectionDecorator<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 class MongoCollectionDecorator<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 class MongoCollectionDecorator<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 class MongoCollectionDecorator<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 class MongoCollectionDecorator<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 class MongoCollectionDecorator<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 update
        update - 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 command
        com.mongodb.MongoWriteConcernException - If the write failed due being unable to fulfill the write concern
        com.mongodb.MongoException - If an error occurred
      • withCodecRegistry

        public com.mongodb.client.MongoCollection<TResult> withCodecRegistry​(org.bson.codecs.configuration.CodecRegistry codecRegistry)
      • 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)