T
- The type that this collection will encode documents from and decode documents to.@ThreadSafe public interface MongoCollection<T>
Note: Additions to this interface will not be considered to break binary compatibility.
Modifier and Type | Method and Description |
---|---|
AggregateIterable<Document> |
aggregate(List<?> pipeline)
Aggregates documents according to the specified aggregation pipeline.
|
<C> AggregateIterable<C> |
aggregate(List<?> pipeline,
Class<C> clazz)
Aggregates documents according to the specified aggregation pipeline.
|
BulkWriteResult |
bulkWrite(List<? extends WriteModel<? extends T>> requests)
Executes a mix of inserts, updates, replaces, and deletes.
|
BulkWriteResult |
bulkWrite(List<? extends WriteModel<? extends T>> requests,
BulkWriteOptions options)
Executes a mix of inserts, updates, replaces, and deletes.
|
long |
count()
Counts the number of documents in the collection.
|
long |
count(Object filter)
Counts the number of documents in the collection according to the given options.
|
long |
count(Object filter,
CountOptions options)
Counts the number of documents in the collection according to the given options.
|
void |
createIndex(Object key) |
void |
createIndex(Object key,
CreateIndexOptions createIndexOptions) |
DeleteResult |
deleteMany(Object filter)
Removes all documents from the collection that match the given query filter.
|
DeleteResult |
deleteOne(Object filter)
Removes at most one document from the collection that matches the given filter.
|
<C> DistinctIterable<C> |
distinct(String fieldName,
Class<C> clazz)
Gets the distinct values of the specified field name.
|
void |
dropCollection()
Drops this collection from the Database.
|
void |
dropIndex(String indexName)
Drops the given index.
|
void |
dropIndexes()
Drop all the indexes on this collection, except for the default on _id.
|
FindIterable<T> |
find()
Finds all documents in the collection.
|
<C> FindIterable<C> |
find(Class<C> clazz)
Finds all documents in the collection.
|
FindIterable<T> |
find(Object filter)
Finds all documents in the collection.
|
<C> FindIterable<C> |
find(Object filter,
Class<C> clazz)
Finds all documents in the collection.
|
T |
findOneAndDelete(Object filter)
Atomically find a document and remove it.
|
T |
findOneAndDelete(Object filter,
FindOneAndDeleteOptions options)
Atomically find a document and remove it.
|
T |
findOneAndReplace(Object filter,
T replacement)
Atomically find a document and replace it.
|
T |
findOneAndReplace(Object filter,
T replacement,
FindOneAndReplaceOptions options)
Atomically find a document and replace it.
|
T |
findOneAndUpdate(Object filter,
Object update)
Atomically find a document and update it.
|
T |
findOneAndUpdate(Object filter,
Object update,
FindOneAndUpdateOptions options)
Atomically find a document and update it.
|
CodecRegistry |
getCodecRegistry()
Get the codec registry for the MongoCollection.
|
Class<T> |
getDefaultClass()
Get the default class to cast any documents returned from the database into.
|
MongoNamespace |
getNamespace()
Gets the namespace of this collection.
|
ReadPreference |
getReadPreference()
Get the read preference for the MongoCollection.
|
WriteConcern |
getWriteConcern()
Get the write concern for the MongoCollection.
|
void |
insertMany(List<? extends T> documents)
Inserts one or more documents.
|
void |
insertMany(List<? extends T> documents,
InsertManyOptions options)
Inserts one or more documents.
|
void |
insertOne(T document)
Inserts the provided document.
|
ListIndexesIterable<Document> |
listIndexes()
Get all the indexes in this collection.
|
<C> ListIndexesIterable<C> |
listIndexes(Class<C> clazz)
Get all the indexes in this collection.
|
MapReduceIterable<Document> |
mapReduce(String mapFunction,
String reduceFunction)
Aggregates documents according to the specified map-reduce function.
|
<C> MapReduceIterable<C> |
mapReduce(String mapFunction,
String reduceFunction,
Class<C> clazz)
Aggregates documents according to the specified map-reduce function.
|
void |
renameCollection(MongoNamespace newCollectionNamespace)
Rename the collection with oldCollectionName to the newCollectionName.
|
void |
renameCollection(MongoNamespace newCollectionNamespace,
RenameCollectionOptions renameCollectionOptions)
Rename the collection with oldCollectionName to the newCollectionName.
|
UpdateResult |
replaceOne(Object filter,
T replacement)
Replace a document in the collection according to the specified arguments.
|
UpdateResult |
replaceOne(Object filter,
T replacement,
UpdateOptions updateOptions)
Replace a document in the collection according to the specified arguments.
|
UpdateResult |
updateMany(Object filter,
Object update)
Update a single document in the collection according to the specified arguments.
|
UpdateResult |
updateMany(Object filter,
Object update,
UpdateOptions updateOptions)
Update a single document in the collection according to the specified arguments.
|
UpdateResult |
updateOne(Object filter,
Object update)
Update a single document in the collection according to the specified arguments.
|
UpdateResult |
updateOne(Object filter,
Object update,
UpdateOptions updateOptions)
Update a single document in the collection according to the specified arguments.
|
MongoCollection<T> |
withCodecRegistry(CodecRegistry codecRegistry)
Create a new MongoCollection instance with a different codec registry.
|
<C> MongoCollection<C> |
withDefaultClass(Class<C> clazz)
Create a new MongoCollection instance with a different default class to cast any documents returned from the database into..
|
MongoCollection<T> |
withReadPreference(ReadPreference readPreference)
Create a new MongoCollection instance with a different read preference.
|
MongoCollection<T> |
withWriteConcern(WriteConcern writeConcern)
Create a new MongoCollection instance with a different write concern.
|
MongoNamespace getNamespace()
Class<T> getDefaultClass()
CodecRegistry getCodecRegistry()
CodecRegistry
ReadPreference getReadPreference()
ReadPreference
WriteConcern getWriteConcern()
WriteConcern
<C> MongoCollection<C> withDefaultClass(Class<C> clazz)
C
- The type that the new collection will encode documents from and decode documents toclazz
- the default class to cast any documents returned from the database into.MongoCollection<T> withCodecRegistry(CodecRegistry codecRegistry)
codecRegistry
- the new CodecRegistry
for the collectionMongoCollection<T> withReadPreference(ReadPreference readPreference)
readPreference
- the new ReadPreference
for the collectionMongoCollection<T> withWriteConcern(WriteConcern writeConcern)
writeConcern
- the new WriteConcern
for the collectionlong count()
long count(Object filter)
filter
- the query filterlong count(Object filter, CountOptions options)
filter
- the query filteroptions
- the options describing the count<C> DistinctIterable<C> distinct(String fieldName, Class<C> clazz)
C
- the target type of the iterable.fieldName
- the field nameclazz
- the default class to cast any distinct items into.FindIterable<T> find()
<C> FindIterable<C> find(Class<C> clazz)
C
- the target document type of the iterable.clazz
- the class to decode each document intoFindIterable<T> find(Object filter)
filter
- the query filter<C> FindIterable<C> find(Object filter, Class<C> clazz)
C
- the target document type of the iterable.filter
- the query filterclazz
- the class to decode each document intoAggregateIterable<Document> aggregate(List<?> pipeline)
pipeline
- the aggregate pipeline<C> AggregateIterable<C> aggregate(List<?> pipeline, Class<C> clazz)
C
- the target document type of the iterable.pipeline
- the aggregate pipelineclazz
- the class to decode each document intoMapReduceIterable<Document> mapReduce(String mapFunction, String reduceFunction)
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.<C> MapReduceIterable<C> mapReduce(String mapFunction, String reduceFunction, Class<C> clazz)
C
- the target document type of the iterable.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.clazz
- the class to decode each resulting document into.BulkWriteResult bulkWrite(List<? extends WriteModel<? extends T>> requests)
requests
- the writes to executeMongoBulkWriteException
- if there's an exception in the bulk write operationMongoException
- if there's an exception running the operationBulkWriteResult bulkWrite(List<? extends WriteModel<? extends T>> requests, BulkWriteOptions options)
requests
- the writes to executeoptions
- the options to apply to the bulk write operationMongoBulkWriteException
- if there's an exception in the bulk write operationMongoException
- if there's an exception running the operationvoid insertOne(T document)
document
- the document to insertMongoWriteException
- if the write failed due some other failure specific to the insert commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failurevoid insertMany(List<? extends T> documents)
bulkWrite
methoddocuments
- the documents to insertMongoBulkWriteException
- if there's an exception in the bulk write operationMongoException
- if the write failed due some other failurebulkWrite(java.util.List<? extends com.mongodb.client.model.WriteModel<? extends T>>)
void insertMany(List<? extends T> documents, InsertManyOptions options)
bulkWrite
methoddocuments
- the documents to insertoptions
- the options to apply to the operationDuplicateKeyException
- if the write failed to a duplicate unique keyWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureDeleteResult deleteOne(Object filter)
filter
- the query filter to apply the the delete operationMongoWriteException
- if the write failed due some other failure specific to the delete commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureDeleteResult deleteMany(Object filter)
filter
- the query filter to apply the the delete operationMongoWriteException
- if the write failed due some other failure specific to the delete commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureUpdateResult replaceOne(Object filter, T replacement)
filter
- the query filter to apply the the replace operationreplacement
- the replacement documentMongoWriteException
- if the write failed due some other failure specific to the replace commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureUpdateResult replaceOne(Object filter, T replacement, UpdateOptions updateOptions)
filter
- the query filter to apply the the replace operationreplacement
- the replacement documentupdateOptions
- the options to apply to the replace operationMongoWriteException
- if the write failed due some other failure specific to the replace commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureUpdateResult updateOne(Object filter, Object update)
filter
- a document describing the query filter, which may not be null. This can be of any type for which a Codec
is
registeredupdate
- a document describing the update, which may not be null. The update to apply must include only update operators. This
can be of any type for which a Codec
is registeredMongoWriteException
- if the write failed due some other failure specific to the update commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureUpdateResult updateOne(Object filter, Object update, UpdateOptions updateOptions)
filter
- a document describing the query filter, which may not be null. This can be of any type for which a Codec
is registeredupdate
- a document describing the update, which may not be null. The update to apply must include only update operators.
This can be of any type for which a Codec
is registeredupdateOptions
- the options to apply to the update operationMongoWriteException
- if the write failed due some other failure specific to the update commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureUpdateResult updateMany(Object filter, Object update)
filter
- a document describing the query filter, which may not be null. This can be of any type for which a Codec
is
registeredupdate
- a document describing the update, which may not be null. The update to apply must include only update operators. This
can be of any type for which a Codec
is registeredMongoWriteException
- if the write failed due some other failure specific to the update commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureUpdateResult updateMany(Object filter, Object update, UpdateOptions updateOptions)
filter
- a document describing the query filter, which may not be null. This can be of any type for which a Codec
is registeredupdate
- a document describing the update, which may not be null. The update to apply must include only update operators.
This can be of any type for which a Codec
is registeredupdateOptions
- the options to apply to the update operationMongoWriteException
- if the write failed due some other failure specific to the update commandMongoWriteConcernException
- if the write failed due being unable to fulfil the write concernMongoException
- if the write failed due some other failureT findOneAndDelete(Object filter)
filter
- the query filter to find the document withT findOneAndDelete(Object filter, FindOneAndDeleteOptions options)
filter
- the query filter to find the document withoptions
- the options to apply to the operationT findOneAndReplace(Object filter, T replacement)
filter
- the query filter to apply the the replace operationreplacement
- the replacement documentreturnOriginal
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
returnedT findOneAndReplace(Object filter, T replacement, FindOneAndReplaceOptions options)
filter
- the query filter to apply the the replace operationreplacement
- the replacement documentoptions
- the options to apply to the operationreturnOriginal
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
returnedT findOneAndUpdate(Object filter, Object update)
filter
- a document describing the query filter, which may not be null. This can be of any type for which a Codec
is
registeredupdate
- a document describing the update, which may not be null. The update to apply must include only update operators. This
can be of any type for which a Codec
is registeredT findOneAndUpdate(Object filter, Object update, FindOneAndUpdateOptions options)
filter
- a document describing the query filter, which may not be null. This can be of any type for which a Codec
is
registeredupdate
- a document describing the update, which may not be null. The update to apply must include only update operators. This
can be of any type for which a Codec
is registeredoptions
- the options to apply to the operationreturnOriginal
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
returnedvoid dropCollection()
void createIndex(Object key)
key
- an object describing the index key(s), which may not be null. This can be of any type for which a Codec
is
registeredvoid createIndex(Object key, CreateIndexOptions createIndexOptions)
key
- an object describing the index key(s), which may not be null. This can be of any type for which a Codec
is registeredcreateIndexOptions
- the options for the indexListIndexesIterable<Document> listIndexes()
<C> ListIndexesIterable<C> listIndexes(Class<C> clazz)
C
- the target document type of the iterable.clazz
- the class to decode each document intovoid dropIndex(String indexName)
indexName
- the name of the index to removevoid dropIndexes()
void renameCollection(MongoNamespace newCollectionNamespace)
newCollectionNamespace
- the namespace the collection will be renamed toMongoServerException
- 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 existvoid renameCollection(MongoNamespace newCollectionNamespace, RenameCollectionOptions renameCollectionOptions)
newCollectionNamespace
- the name the collection will be renamed torenameCollectionOptions
- the options for renaming a collectionMongoServerException
- 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