Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
com.mongodb.client

Interface MongoCollection<TDocument>

  • Type Parameters:
    TDocument - The type that this collection will encode documents from and decode documents to.


    @ThreadSafe
    public interface MongoCollection<TDocument>
    The MongoCollection interface.

    Note: Additions to this interface will not be considered to break binary compatibility.

    MongoCollection is generic allowing for different types to represent documents. Any custom classes must have a Codec registered in the CodecRegistry. The default CodecRegistry includes built-in support for: BsonDocument, Document and DBObject.

    Since:
    3.0
    • Method Detail

      • getNamespace

        MongoNamespace getNamespace()
        Gets the namespace of this collection.
        Returns:
        the namespace
      • getDocumentClass

        Class<TDocument> getDocumentClass()
        Get the class of documents stored in this collection.
        Returns:
        the class
      • getWriteConcern

        WriteConcern getWriteConcern()
        Get the write concern for the MongoCollection.
        Returns:
        the WriteConcern
      • getReadConcern

        ReadConcern getReadConcern()
        Get the read concern for the MongoCollection.
        Returns:
        the ReadConcern
        Since:
        3.2
        MongoDB documentation
        Read Concern
        Since server release
        3.2
      • withDocumentClass

        <NewTDocument> MongoCollection<NewTDocument> withDocumentClass(Class<NewTDocument> clazz)
        Create a new MongoCollection instance with a different default class to cast any documents returned from the database into..
        Type Parameters:
        NewTDocument - The type that the new collection will encode documents from and decode documents to
        Parameters:
        clazz - the default class to cast any documents returned from the database into.
        Returns:
        a new MongoCollection instance with the different default class
      • withCodecRegistry

        MongoCollection<TDocument> withCodecRegistry(CodecRegistry codecRegistry)
        Create a new MongoCollection instance with a different codec registry.
        Parameters:
        codecRegistry - the new CodecRegistry for the collection
        Returns:
        a new MongoCollection instance with the different codec registry
      • withReadPreference

        MongoCollection<TDocument> withReadPreference(ReadPreference readPreference)
        Create a new MongoCollection instance with a different read preference.
        Parameters:
        readPreference - the new ReadPreference for the collection
        Returns:
        a new MongoCollection instance with the different readPreference
      • withWriteConcern

        MongoCollection<TDocument> withWriteConcern(WriteConcern writeConcern)
        Create a new MongoCollection instance with a different write concern.
        Parameters:
        writeConcern - the new WriteConcern for the collection
        Returns:
        a new MongoCollection instance with the different writeConcern
      • withReadConcern

        MongoCollection<TDocument> withReadConcern(ReadConcern readConcern)
        Create a new MongoCollection instance with a different read concern.
        Parameters:
        readConcern - the new ReadConcern for the collection
        Returns:
        a new MongoCollection instance with the different ReadConcern
        Since:
        3.2
        MongoDB documentation
        Read Concern
        Since server release
        3.2
      • count

        long count()
        Counts the number of documents in the collection.
        Returns:
        the number of documents in the collection
      • count

        long count(Bson filter)
        Counts the number of documents in the collection according to the given options.
        Parameters:
        filter - the query filter
        Returns:
        the number of documents in the collection
      • count

        long count(Bson filter,
                   CountOptions options)
        Counts the number of documents in the collection according to the given options.
        Parameters:
        filter - the query filter
        options - the options describing the count
        Returns:
        the number of documents in the collection
      • distinct

        <TResult> DistinctIterable<TResult> distinct(String fieldName,
                                                     Class<TResult> resultClass)
        Gets the distinct values of the specified field name.
        Type Parameters:
        TResult - the target type of the iterable.
        Parameters:
        fieldName - the field name
        resultClass - the class to cast any distinct items into.
        Returns:
        an iterable of distinct values
        MongoDB documentation
        Distinct
      • distinct

        <TResult> DistinctIterable<TResult> distinct(String fieldName,
                                                     Bson filter,
                                                     Class<TResult> resultClass)
        Gets the distinct values of the specified field name.
        Type Parameters:
        TResult - the target type of the iterable.
        Parameters:
        fieldName - the field name
        filter - the query filter
        resultClass - the class to cast any distinct items into.
        Returns:
        an iterable of distinct values
        MongoDB documentation
        Distinct
      • find

        FindIterable<TDocument> find()
        Finds all documents in the collection.
        Returns:
        the find iterable interface
        MongoDB documentation
        Find
      • find

        <TResult> FindIterable<TResult> find(Class<TResult> resultClass)
        Finds all documents in the collection.
        Type Parameters:
        TResult - the target document type of the iterable.
        Parameters:
        resultClass - the class to decode each document into
        Returns:
        the find iterable interface
        MongoDB documentation
        Find
      • find

        FindIterable<TDocument> find(Bson filter)
        Finds all documents in the collection.
        Parameters:
        filter - the query filter
        Returns:
        the find iterable interface
        MongoDB documentation
        Find
      • find

        <TResult> FindIterable<TResult> find(Bson filter,
                                             Class<TResult> resultClass)
        Finds all documents in the collection.
        Type Parameters:
        TResult - the target document type of the iterable.
        Parameters:
        filter - the query filter
        resultClass - the class to decode each document into
        Returns:
        the find iterable interface
        MongoDB documentation
        Find
      • aggregate

        AggregateIterable<TDocument> aggregate(List<? extends Bson> pipeline)
        Aggregates documents according to the specified aggregation pipeline.
        Parameters:
        pipeline - the aggregate pipeline
        Returns:
        an iterable containing the result of the aggregation operation
        MongoDB documentation
        Aggregation
        Since server release
        2.2
      • aggregate

        <TResult> AggregateIterable<TResult> aggregate(List<? extends Bson> pipeline,
                                                       Class<TResult> resultClass)
        Aggregates documents according to the specified aggregation pipeline.
        Type Parameters:
        TResult - the target document type of the iterable.
        Parameters:
        pipeline - the aggregate pipeline
        resultClass - the class to decode each document into
        Returns:
        an iterable containing the result of the aggregation operation
        MongoDB documentation
        Aggregation
        Since server release
        2.2
      • mapReduce

        MapReduceIterable<TDocument> mapReduce(String mapFunction,
                                               String reduceFunction)
        Aggregates documents according to the specified map-reduce function.
        Parameters:
        mapFunction - A JavaScript function that associates or "maps" a value with a key and emits the key and value pair.
        reduceFunction - A JavaScript function that "reduces" to a single object all the values associated with a particular key.
        Returns:
        an iterable containing the result of the map-reduce operation
        MongoDB documentation
        map-reduce
      • mapReduce

        <TResult> MapReduceIterable<TResult> mapReduce(String mapFunction,
                                                       String reduceFunction,
                                                       Class<TResult> resultClass)
        Aggregates documents according to the specified map-reduce function.
        Type Parameters:
        TResult - the target document type of the iterable.
        Parameters:
        mapFunction - A JavaScript function that associates or "maps" a value with a key and emits the key and value pair.
        reduceFunction - A JavaScript function that "reduces" to a single object all the values associated with a particular key.
        resultClass - the class to decode each resulting document into.
        Returns:
        an iterable containing the result of the map-reduce operation
        MongoDB documentation
        map-reduce
      • bulkWrite

        BulkWriteResult bulkWrite(List<? extends WriteModel<? extends TDocument>> requests)
        Executes a mix of inserts, updates, replaces, and deletes.
        Parameters:
        requests - the writes to execute
        Returns:
        the result of the bulk write
        Throws:
        MongoBulkWriteException - if there's an exception in the bulk write operation
        MongoException - if there's an exception running the operation
      • bulkWrite

        BulkWriteResult bulkWrite(List<? extends WriteModel<? extends TDocument>> requests,
                                  BulkWriteOptions options)
        Executes a mix of inserts, updates, replaces, and deletes.
        Parameters:
        requests - the writes to execute
        options - the options to apply to the bulk write operation
        Returns:
        the result of the bulk write
        Throws:
        MongoBulkWriteException - if there's an exception in the bulk write operation
        MongoException - if there's an exception running the operation
      • insertOne

        void insertOne(TDocument document)
        Inserts the provided document. If the document is missing an identifier, the driver should generate one.
        Parameters:
        document - the document to insert
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the insert command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
      • insertOne

        void insertOne(TDocument document,
                       InsertOneOptions options)
        Inserts the provided document. If the document is missing an identifier, the driver should generate one.
        Parameters:
        document - the document to insert
        options - the options to apply to the operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the insert command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoCommandException - if the write failed due to document validation reasons
        MongoException - if the write failed due some other failure
        Since:
        3.2
      • insertMany

        void insertMany(List<? extends TDocument> documents,
                        InsertManyOptions options)
        Inserts one or more documents. A call to this method is equivalent to a call to the bulkWrite method
        Parameters:
        documents - the documents to insert
        options - the options to apply to the operation
        Throws:
        DuplicateKeyException - if the write failed to a duplicate unique key
        WriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
      • deleteOne

        DeleteResult deleteOne(Bson filter)
        Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not modified.
        Parameters:
        filter - the query filter to apply the the delete operation
        Returns:
        the result of the remove one operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the delete command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
      • deleteOne

        DeleteResult deleteOne(Bson filter,
                               DeleteOptions options)
        Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not modified.
        Parameters:
        filter - the query filter to apply the the delete operation
        options - the options to apply to the delete operation
        Returns:
        the result of the remove one operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the delete command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
        Since:
        3.4
      • deleteMany

        DeleteResult deleteMany(Bson filter)
        Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.
        Parameters:
        filter - the query filter to apply the the delete operation
        Returns:
        the result of the remove many operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the delete command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
      • deleteMany

        DeleteResult deleteMany(Bson filter,
                                DeleteOptions options)
        Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.
        Parameters:
        filter - the query filter to apply the the delete operation
        options - the options to apply to the delete operation
        Returns:
        the result of the remove many operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the delete command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
        Since:
        3.4
      • replaceOne

        UpdateResult replaceOne(Bson filter,
                                TDocument replacement)
        Replace a document in the collection according to the specified arguments.
        Parameters:
        filter - the query filter to apply the the replace operation
        replacement - the replacement document
        Returns:
        the result of the replace one operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the replace command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
        MongoDB documentation
        Replace
      • replaceOne

        UpdateResult replaceOne(Bson filter,
                                TDocument replacement,
                                UpdateOptions updateOptions)
        Replace a document in the collection according to the specified arguments.
        Parameters:
        filter - the query filter to apply the the replace operation
        replacement - the replacement document
        updateOptions - the options to apply to the replace operation
        Returns:
        the result of the replace one operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the replace command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
        MongoDB documentation
        Replace
      • updateOne

        UpdateResult updateOne(Bson filter,
                               Bson update)
        Update a single document in the collection according to the specified arguments.
        Parameters:
        filter - a document describing the query filter, which may not be null.
        update - a document describing the update, which may not be null. The update to apply must include only update operators.
        Returns:
        the result of the update one operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the update command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
        MongoDB documentation
        Updates
        Update Operators
      • updateOne

        UpdateResult updateOne(Bson filter,
                               Bson update,
                               UpdateOptions updateOptions)
        Update a single document in the collection according to the specified arguments.
        Parameters:
        filter - a document describing the query filter, which may not be null.
        update - a document describing the update, which may not be null. The update to apply must include only update operators.
        updateOptions - the options to apply to the update operation
        Returns:
        the result of the update one operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the update command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
        MongoDB documentation
        Updates
        Update Operators
      • updateMany

        UpdateResult updateMany(Bson filter,
                                Bson update)
        Update all documents in the collection according to the specified arguments.
        Parameters:
        filter - a document describing the query filter, which may not be null.
        update - a document describing the update, which may not be null. The update to apply must include only update operators.
        Returns:
        the result of the update many operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the update command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
        MongoDB documentation
        Updates
        Update Operators
      • updateMany

        UpdateResult updateMany(Bson filter,
                                Bson update,
                                UpdateOptions updateOptions)
        Update all documents in the collection according to the specified arguments.
        Parameters:
        filter - a document describing the query filter, which may not be null.
        update - a document describing the update, which may not be null. The update to apply must include only update operators.
        updateOptions - the options to apply to the update operation
        Returns:
        the result of the update many operation
        Throws:
        MongoWriteException - if the write failed due some other failure specific to the update command
        MongoWriteConcernException - if the write failed due being unable to fulfil the write concern
        MongoException - if the write failed due some other failure
        MongoDB documentation
        Updates
        Update Operators
      • findOneAndDelete

        TDocument findOneAndDelete(Bson filter)
        Atomically find a document and remove it.
        Parameters:
        filter - the query filter to find the document with
        Returns:
        the document that was removed. If no documents matched the query filter, then null will be returned
      • findOneAndDelete

        TDocument findOneAndDelete(Bson filter,
                                   FindOneAndDeleteOptions options)
        Atomically find a document and remove it.
        Parameters:
        filter - the query filter to find the document with
        options - the options to apply to the operation
        Returns:
        the document that was removed. If no documents matched the query filter, then null will be returned
      • findOneAndReplace

        TDocument findOneAndReplace(Bson filter,
                                    TDocument replacement)
        Atomically find a document and replace it.
        Parameters:
        filter - the query filter to apply the the replace operation
        replacement - the replacement document
        Returns:
        the document that was replaced. Depending on the value of the returnOriginal property, this will either be the document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be returned
      • findOneAndReplace

        TDocument findOneAndReplace(Bson filter,
                                    TDocument replacement,
                                    FindOneAndReplaceOptions options)
        Atomically find a document and replace it.
        Parameters:
        filter - the query filter to apply the the replace operation
        replacement - the replacement document
        options - the options to apply to the operation
        Returns:
        the document that was replaced. Depending on the value of the returnOriginal property, this will either be the document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be returned
      • findOneAndUpdate

        TDocument findOneAndUpdate(Bson filter,
                                   Bson update)
        Atomically find a document and update it.
        Parameters:
        filter - a document describing the query filter, which may not be null.
        update - a document describing the update, which may not be null. The update to apply must include only update operators.
        Returns:
        the document that was updated before the update was applied. If no documents matched the query filter, then null will be returned
      • findOneAndUpdate

        TDocument findOneAndUpdate(Bson filter,
                                   Bson update,
                                   FindOneAndUpdateOptions options)
        Atomically find a document and update it.
        Parameters:
        filter - a document describing the query filter, which may not be null.
        update - a document describing the update, which may not be null. The update to apply must include only update operators.
        options - the options to apply to the operation
        Returns:
        the document that was updated. Depending on the value of the returnOriginal property, this will either be the document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be returned
      • drop

        void drop()
        Drops this collection from the Database.
        MongoDB documentation
        Drop Collection
      • createIndex

        String createIndex(Bson keys)
        Create an index with the given keys.
        Parameters:
        keys - an object describing the index key(s), which may not be null.
        Returns:
        the index name
        MongoDB documentation
        Create indexes
      • createIndex

        String createIndex(Bson keys,
                           IndexOptions indexOptions)
        Create an index with the given keys and options.
        Parameters:
        keys - an object describing the index key(s), which may not be null.
        indexOptions - the options for the index
        Returns:
        the index name
        MongoDB documentation
        Create indexes
      • createIndexes

        List<String> createIndexes(List<IndexModel> indexes)
        Create multiple indexes.
        Parameters:
        indexes - the list of indexes
        Returns:
        the list of index names
        MongoDB documentation
        Create indexes
        Since server release
        2.6
      • listIndexes

        <TResult> ListIndexesIterable<TResult> listIndexes(Class<TResult> resultClass)
        Get all the indexes in this collection.
        Type Parameters:
        TResult - the target document type of the iterable.
        Parameters:
        resultClass - the class to decode each document into
        Returns:
        the list indexes iterable interface
        MongoDB documentation
        List indexes
      • dropIndex

        void dropIndex(String indexName)
        Drops the index given its name.
        Parameters:
        indexName - the name of the index to remove
        MongoDB documentation
        Drop indexes
      • dropIndex

        void dropIndex(Bson keys)
        Drops the index given the keys used to create it.
        Parameters:
        keys - the keys of the index to remove
        MongoDB documentation
        Drop indexes
      • dropIndexes

        void dropIndexes()
        Drop all the indexes on this collection, except for the default on _id.
        MongoDB documentation
        Drop indexes
      • renameCollection

        void renameCollection(MongoNamespace newCollectionNamespace)
        Rename the collection with oldCollectionName to the newCollectionName.
        Parameters:
        newCollectionNamespace - the namespace the collection will be renamed to
        Throws:
        MongoServerException - if you provide a newCollectionName that is the name of an existing collection, or if the oldCollectionName is the name of a collection that doesn't exist
        MongoDB documentation
        Rename collection
      • renameCollection

        void renameCollection(MongoNamespace newCollectionNamespace,
                              RenameCollectionOptions renameCollectionOptions)
        Rename the collection with oldCollectionName to the newCollectionName.
        Parameters:
        newCollectionNamespace - the name the collection will be renamed to
        renameCollectionOptions - the options for renaming a collection
        Throws:
        MongoServerException - if you provide a newCollectionName that is the name of an existing collection and dropTarget is false, or if the oldCollectionName is the name of a collection that doesn't exist
        MongoDB documentation
        Rename collection