![]() |
千杯不醉的拖把 · 如何在Java中将我的参数拆分为Proces ...· 9 月前 · |
![]() |
纯真的保温杯 · 从JSON中获取最大值· 9 月前 · |
![]() |
打篮球的铁链 · Cobaltstrike内网渗透神器入门使用 ...· 1 年前 · |
![]() |
爱健身的瀑布 · python爬虫(19)爬取论坛网站——网络 ...· 1 年前 · |
![]() |
活泼的黄花菜 · PDOStatement::fetch - ...· 1 年前 · |
string mongodb 留学 update |
https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoTemplate.html |
![]() |
无邪的圣诞树
1 年前 |
Aware
,
ApplicationContextAware
,
ExecutableAggregationOperation
,
ExecutableFindOperation
,
ExecutableInsertOperation
,
ExecutableMapReduceOperation
,
ExecutableRemoveOperation
,
ExecutableUpdateOperation
,
FluentMongoOperations
,
IndexOperationsProvider
,
MongoOperations
MongoOperations
.
ExecutableAggregationOperation.AggregationWithAggregation
<
T
>,
ExecutableAggregationOperation.AggregationWithCollection
<
T
>,
ExecutableAggregationOperation.ExecutableAggregation
<
T
>,
ExecutableAggregationOperation.TerminatingAggregation
<
T
>
ExecutableFindOperation.DistinctWithProjection
,
ExecutableFindOperation.DistinctWithQuery
<
T
>,
ExecutableFindOperation.ExecutableFind
<
T
>,
ExecutableFindOperation.FindDistinct
,
ExecutableFindOperation.FindWithCollection
<
T
>,
ExecutableFindOperation.FindWithProjection
<
T
>,
ExecutableFindOperation.FindWithQuery
<
T
>,
ExecutableFindOperation.TerminatingDistinct
<
T
>,
ExecutableFindOperation.TerminatingFind
<
T
>,
ExecutableFindOperation.TerminatingFindNear
<
T
>
ExecutableInsertOperation.ExecutableInsert
<
T
>,
ExecutableInsertOperation.InsertWithBulkMode
<
T
>,
ExecutableInsertOperation.InsertWithCollection
<
T
>,
ExecutableInsertOperation.TerminatingBulkInsert
<
T
>,
ExecutableInsertOperation.TerminatingInsert
<
T
>
ExecutableMapReduceOperation.ExecutableMapReduce
<
T
>,
ExecutableMapReduceOperation.MapReduceWithCollection
<
T
>,
ExecutableMapReduceOperation.MapReduceWithMapFunction
<
T
>,
ExecutableMapReduceOperation.MapReduceWithOptions
<
T
>,
ExecutableMapReduceOperation.MapReduceWithProjection
<
T
>,
ExecutableMapReduceOperation.MapReduceWithQuery
<
T
>,
ExecutableMapReduceOperation.MapReduceWithReduceFunction
<
T
>,
ExecutableMapReduceOperation.TerminatingMapReduce
<
T
>
ExecutableRemoveOperation.ExecutableRemove
<
T
>,
ExecutableRemoveOperation.RemoveWithCollection
<
T
>,
ExecutableRemoveOperation.RemoveWithQuery
<
T
>,
ExecutableRemoveOperation.TerminatingRemove
<
T
>
ExecutableUpdateOperation.ExecutableUpdate
<
T
>,
ExecutableUpdateOperation.FindAndModifyWithOptions
<
T
>,
ExecutableUpdateOperation.FindAndReplaceWithOptions
<
T
>,
ExecutableUpdateOperation.FindAndReplaceWithProjection
<
T
>,
ExecutableUpdateOperation.TerminatingFindAndModify
<
T
>,
ExecutableUpdateOperation.TerminatingFindAndReplace
<
T
>,
ExecutableUpdateOperation.TerminatingUpdate
<
T
>,
ExecutableUpdateOperation.UpdateWithCollection
<
T
>,
ExecutableUpdateOperation.UpdateWithQuery
<
T
>,
ExecutableUpdateOperation.UpdateWithUpdate
<
T
>
MongoTemplate
(com.mongodb.client.MongoClient mongoClient,
String
databaseName)
MongoTemplate
(
MongoDatabaseFactory
mongoDbFactory)
MongoTemplate
(
MongoDatabaseFactory
mongoDbFactory,
MongoConverter
mongoConverter)
protected <O>
AggregationResults
<O>
aggregate
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType,
AggregationOperationContext
context)
<O>
AggregationResults
<O>
aggregate
(
TypedAggregation
<?> aggregation,
Class
<O> outputType)
<O>
AggregationResults
<O>
aggregate
(
TypedAggregation
<?> aggregation,
String
inputCollectionName,
Class
<O> outputType)
aggregateAndReturn
(
Class
<T> domainType)
<O>
Stream
<O>
aggregateStream
(
Aggregation
aggregation,
Class
<?> inputType,
Class
<O> outputType)
AggregateIterable
.
<O>
Stream
<O>
aggregateStream
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType)
AggregateIterable
.
protected <O>
Stream
<O>
aggregateStream
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType,
AggregationOperationContext
context)
<O>
Stream
<O>
aggregateStream
(
TypedAggregation
<?> aggregation,
Class
<O> outputType)
AggregateIterable
.
<O>
Stream
<O>
aggregateStream
(
TypedAggregation
<?> aggregation,
String
inputCollectionName,
Class
<O> outputType)
AggregateIterable
.
bulkOps
(
BulkOperations.BulkMode
bulkMode,
Class
<?> entityClass)
BulkOperations
for the given entity type.
bulkOps
(
BulkOperations.BulkMode
mode,
Class
<?> entityType,
String
collectionName)
BulkOperations
for the given entity type and collection name.
bulkOps
(
BulkOperations.BulkMode
bulkMode,
String
collectionName)
BulkOperations
for the given collection.
<T> boolean
collectionExists
(
Class
<T> entityClass)
boolean
collectionExists
(
String
collectionName)
protected org.bson.Document
convertToDocument
(
CollectionOptions
collectionOptions,
Class
<?> targetType)
CollectionOptions
to a document and take the domain type information into account when
creating a mapped schema for validation.
Query
by querying the collection of the given entity class.
Query
by querying the given collection using the given entity
class to map the given
Query
.
Query
querying the given collection.
protected boolean
countCanBeEstimated
(org.bson.Document filter,
com.mongodb.client.model.CountOptions options)
<T> com.mongodb.client.MongoCollection<org.bson.Document>
createCollection
(
Class
<T> entityClass)
<T> com.mongodb.client.MongoCollection<org.bson.Document>
createCollection
(
Class
<T> entityClass,
CollectionOptions
collectionOptions)
com.mongodb.client.MongoCollection<org.bson.Document>
createCollection
(
String
collectionName)
com.mongodb.client.MongoCollection<org.bson.Document>
createCollection
(
String
collectionName,
CollectionOptions
collectionOptions)
com.mongodb.client.MongoCollection<org.bson.Document>
createView
(
String
name,
Class
<?> source,
AggregationPipeline
pipeline,
ViewOptions
options)
com.mongodb.client.MongoCollection<org.bson.Document>
createView
(
String
name,
String
source,
AggregationPipeline
pipeline,
ViewOptions
options)
protected <O>
AggregationResults
<O>
doAggregate
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType,
AggregationOperationContext
context)
protected long
doCount
(
String
collectionName,
org.bson.Document filter,
com.mongodb.client.model.CountOptions options)
protected com.mongodb.client.MongoCollection<org.bson.Document>
doCreateCollection
(
String
collectionName,
com.mongodb.client.model.CreateCollectionOptions collectionOptions)
protected com.mongodb.client.MongoCollection<org.bson.Document>
doCreateCollection
(
String
collectionName,
org.bson.Document collectionOptions)
protected com.mongodb.client.MongoCollection<org.bson.Document>
doCreateView
(
String
name,
String
source,
List
<org.bson.Document> pipeline,
ViewOptions
options)
protected long
doEstimatedCount
(
String
collectionName,
com.mongodb.client.model.EstimatedDocumentCountOptions options)
protected long
doExactCount
(
String
collectionName,
org.bson.Document filter,
com.mongodb.client.model.CountOptions options)
protected <S,
T>
List
<T>
doFind
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
Class
<S> entityClass,
CursorPreparer
preparer,
org.springframework.data.mongodb.core.MongoTemplate.DocumentCallback<T> objectCallback)
protected <T>
List
<T>
doFind
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
Class
<T> entityClass)
protected <T>
List
<T>
doFind
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
Class
<T> entityClass,
CursorPreparer
preparer)
protected <T>
List
<T>
doFindAndDelete
(
String
collectionName,
Query
query,
Class
<T> entityClass)
query
by calling
find(Query, Class, String)
and
remove(Query, Class, String)
, whereas the
Query
for
remove(Query, Class, String)
is
constructed out of the find result.
protected <T> T
doFindAndModify
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
org.bson.Document sort,
Class
<T> entityClass,
UpdateDefinition
update,
FindAndModifyOptions
options)
protected <T> T
doFindAndRemove
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
org.bson.Document sort,
Collation
collation,
Class
<T> entityClass)
protected <T> T
doFindAndReplace
(
String
collectionName,
org.bson.Document mappedQuery,
org.bson.Document mappedFields,
org.bson.Document mappedSort,
com.mongodb.client.model.Collation collation,
Class
<?> entityType,
org.bson.Document replacement,
FindAndReplaceOptions
options,
Class
<T> resultType)
protected <T> T
doFindOne
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
Class
<T> entityClass)
protected <T> T
doFindOne
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
CursorPreparer
preparer,
Class
<T> entityClass)
protected com.mongodb.client.MongoDatabase
protected <T> T
doInsert
(
String
collectionName,
T objectToSave,
MongoWriter
<T> writer)
protected <T>
Collection
<T>
doInsertAll
(
Collection
<? extends T> listToSave,
MongoWriter
<T> writer)
protected <T>
Collection
<T>
doInsertBatch
(
String
collectionName,
Collection
<? extends T> batchToSave,
MongoWriter
<T> writer)
protected <T> com.mongodb.client.result.DeleteResult
protected <T> T
doSave
(
String
collectionName,
T objectToSave,
MongoWriter
<T> writer)
protected <T>
Stream
<T>
protected com.mongodb.client.result.UpdateResult
doUpdate
(
String
collectionName,
Query
query,
UpdateDefinition
update,
Class
<?> entityClass,
boolean upsert,
boolean multi)
<T> void
dropCollection
(
Class
<T> entityClass)
dropCollection
(
String
collectionName)
protected void
ensureNotCollectionLike
(
Object
source)
estimatedCount
(
String
collectionName)
exactCount
(
Query
query,
Class
<?> entityClass,
String
collectionName)
Query
by querying the given collection using the given entity
class to map the given
Query
.
execute
(
Class
<?> entityClass,
CollectionCallback
<T> callback)
CollectionCallback
on the entity collection of the specified class.
execute
(
String
collectionName,
CollectionCallback
<T> callback)
CollectionCallback
on the collection of the given name.
execute
(
DbCallback
<T> action)
DbCallback
translating any exceptions as necessary.
org.bson.Document
executeCommand
(
String
jsonCommand)
org.bson.Document
executeCommand
(org.bson.Document command)
org.bson.Document
executeCommand
(org.bson.Document command,
com.mongodb.ReadPreference readPreference)
executeQuery
(
Query
query,
String
collectionName,
DocumentCallbackHandler
dch)
protected void
executeQuery
(
Query
query,
String
collectionName,
DocumentCallbackHandler
documentCallbackHandler,
CursorPreparer
preparer)
DocumentCallbackHandler
using the provided CursorPreparer.
boolean
Query
contains at least one element.
boolean
Query
contains at least one element.
boolean
Query
contains at least one element.
<T>
List
<T>
<T>
List
<T>
<T>
List
<T>
<T>
List
<T>
<T>
List
<T>
findAllAndRemove
(
Query
query,
Class
<T> entityClass)
<T>
List
<T>
findAllAndRemove
(
Query
query,
Class
<T> entityClass,
String
collectionName)
<T>
List
<T>
findAllAndRemove
(
Query
query,
String
collectionName)
findAndModify
(
Query
query,
UpdateDefinition
update,
Class
<T> entityClass)
findAndModify
(
Query
query,
UpdateDefinition
update,
Class
<T> entityClass,
String
collectionName)
findAndModify
(
Query
query,
UpdateDefinition
update,
FindAndModifyOptions
options,
Class
<T> entityClass)
Update
on documents matching
Criteria
of given
Query
taking
FindAndModifyOptions
into account.
findAndModify
(
Query
query,
UpdateDefinition
update,
FindAndModifyOptions
options,
Class
<T> entityClass,
String
collectionName)
Update
on documents matching
Criteria
of given
Query
taking
FindAndModifyOptions
into account.
findAndRemove
(
Query
query,
Class
<T> entityClass)
findAndRemove
(
Query
query,
Class
<T> entityClass,
String
collectionName)
findAndReplace
(
Query
query,
S replacement,
FindAndReplaceOptions
options,
Class
<S> entityType,
String
collectionName,
Class
<T> resultType)
Criteria
of given
Query
with the
replacement
document
taking
FindAndReplaceOptions
into account.
<T>
List
<T>
findDistinct
(
Query
query,
String
field,
Class
<?> entityClass,
Class
<T> resultClass)
MongoCollection
or view and
returns the results in a
List
.
<T>
List
<T>
findDistinct
(
Query
query,
String
field,
String
collectionName,
Class
<?> entityClass,
Class
<T> resultClass)
MongoCollection
or view and
returns the results in a
List
.
<T>
GeoResults
<T>
<T>
GeoResults
<T>
GeoResults
for all entities matching the given
NearQuery
.
<T>
GeoResults
<T>
GeoResults
for all entities matching the given
NearQuery
.
com.mongodb.client.MongoCollection<org.bson.Document>
getCollection
(
String
collectionName)
MongoCollection
by its name.
getCollectionName
(
Class
<?> entityClass)
getConverter
()
MongoConverter
.
com.mongodb.client.MongoDatabase
getDb
()
<T>
Collection
<T>
insert
(
Collection
<? extends T> batchToSave,
Class
<?> entityClass)
<T>
Collection
<T>
insert
(
Collection
<? extends T> batchToSave,
String
collectionName)
insert
(T objectToSave)
<T>
Collection
<T>
insertAll
(
Collection
<? extends T> objectsToSave)
protected
Object
insertDocument
(
String
collectionName,
org.bson.Document document,
Class
<?> entityClass)
insertDocumentList
(
String
collectionName,
List
<org.bson.Document> documents)
<T>
MapReduceResults
<T>
mapReduce
(
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
Class
<T> entityClass)
<T>
MapReduceResults
<T>
mapReduce
(
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
MapReduceOptions
mapReduceOptions,
Class
<T> entityClass)
<T>
List
<T>
mapReduce
(
Query
query,
Class
<?> domainType,
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
MapReduceOptions
mapReduceOptions,
Class
<T> resultType)
<T>
MapReduceResults
<T>
mapReduce
(
Query
query,
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
Class
<T> entityClass)
<T>
MapReduceResults
<T>
mapReduce
(
Query
query,
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
MapReduceOptions
mapReduceOptions,
Class
<T> entityClass)
protected <T> T
maybeCallAfterConvert
(T object,
org.bson.Document document,
String
collection)
protected <T> T
maybeCallAfterSave
(T object,
org.bson.Document document,
String
collection)
protected <T> T
maybeCallBeforeConvert
(T object,
String
collection)
protected <T> T
maybeCallBeforeSave
(T object,
org.bson.Document document,
String
collection)
protected <E extends
MongoMappingEvent
<T>,
maybeEmitEvent
(E event)
protected <T> T
populateIdIfNecessary
(T savedObject,
Object
id)
Populates the id property of the saved object, if it's not set already.
protected com.mongodb.client.MongoCollection<org.bson.Document>
prepareCollection
(com.mongodb.client.MongoCollection<org.bson.Document> collection)
Prepare the collection before any processing is done using it.
protected com.mongodb.client.MongoDatabase
prepareDatabase
(com.mongodb.client.MongoDatabase database)
protected com.mongodb.WriteConcern
prepareWriteConcern
(
MongoAction
mongoAction)
Prepare the WriteConcern before any processing is done using it.
Start creating a find operation for the given domainType.
Start creating a remove operation for the given domainType.
com.mongodb.client.result.DeleteResult
Remove the given object from the collection by id and (if applicable) its
Version
.
com.mongodb.client.result.DeleteResult
Removes the given object from the given collection by id and (if applicable) its
Version
.
com.mongodb.client.result.DeleteResult
Remove all documents that match the provided query document criteria from the collection used to store the
entityClass.
com.mongodb.client.result.DeleteResult
Remove all documents that match the provided query document criteria from the collection used to store the
entityClass.
com.mongodb.client.result.DeleteResult
Remove all documents from the specified collection that match the provided query document criteria.
protected
String
replaceWithResourceIfNecessary
(
String
function)
save
(T objectToSave)
Save the object to the collection for the entity type of the object to save.
Save the object to the specified collection.
protected
Object
saveDocument
(
String
collectionName,
org.bson.Document dbDoc,
Class
<?> entityClass)
scriptOps
()
Returns the
ScriptOperations
that can be performed on
MongoDatabase
level.
setApplicationContext
(
ApplicationContext
applicationContext)
setEntityCallbacks
(
EntityCallbacks
entityCallbacks)
setEntityLifecycleEventsEnabled
(boolean enabled)
Configure whether lifecycle events such as
AfterLoadEvent
,
BeforeSaveEvent
, etc. should be
published or whether emission should be suppressed.
setReadPreference
(com.mongodb.ReadPreference readPreference)
Used by @{link
prepareCollection(MongoCollection)
to set the
ReadPreference
before any operations
are performed.
setSessionSynchronization
(
SessionSynchronization
sessionSynchronization)
Define if
MongoTemplate
should participate in transactions.
setWriteConcern
(com.mongodb.WriteConcern writeConcern)
Configures the
WriteConcern
to be used with the template.
setWriteConcernResolver
(
WriteConcernResolver
writeConcernResolver)
Configures the
WriteConcernResolver
to be used with the template.
setWriteResultChecking
(
WriteResultChecking
resultChecking)
Configures the
WriteResultChecking
to be used with the template.
<T>
Stream
<T>
Executes the given
Query
on the entity collection of the specified
entityType
backed by a Mongo DB
FindIterable
.
<T>
Stream
<T>
Executes the given
Query
on the entity collection of the specified
entityType
and collection backed
by a Mongo DB
FindIterable
.
Start creating an update operation for the given domainType.
com.mongodb.client.result.UpdateResult
updateFirst
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass)
Updates the first object that is found in the collection of the entity class that matches the query document with
the provided update document.
com.mongodb.client.result.UpdateResult
updateFirst
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass,
String
collectionName)
Updates the first object that is found in the specified collection that matches the query document criteria with
the provided updated document.
com.mongodb.client.result.UpdateResult
updateFirst
(
Query
query,
UpdateDefinition
update,
String
collectionName)
Updates the first object that is found in the specified collection that matches the query document criteria with
the provided updated document.
com.mongodb.client.result.UpdateResult
updateMulti
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass)
Updates all objects that are found in the collection for the entity class that matches the query document criteria
with the provided updated document.
com.mongodb.client.result.UpdateResult
updateMulti
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass,
String
collectionName)
Updates all objects that are found in the collection for the entity class that matches the query document criteria
with the provided updated document.
com.mongodb.client.result.UpdateResult
updateMulti
(
Query
query,
UpdateDefinition
update,
String
collectionName)
Updates all objects that are found in the specified collection that matches the query document criteria with the
provided updated document.
com.mongodb.client.result.UpdateResult
upsert
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass)
Performs an upsert.
com.mongodb.client.result.UpdateResult
upsert
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass,
String
collectionName)
Performs an upsert.
com.mongodb.client.result.UpdateResult
upsert
(
Query
query,
UpdateDefinition
update,
String
collectionName)
Performs an upsert.
useEstimatedCount
(boolean enabled)
Configure whether to use estimated count.
withSession
(com.mongodb.client.ClientSession session)
Obtain a
ClientSession
bound instance of
MongoOperations
.
withSession
(com.mongodb.ClientSessionOptions options)
Obtain a
session
bound instance of
SessionScoped
binding a new
ClientSession
with given sessionOptions to each and every command issued against MongoDB.
Methods inherited from class java.lang.
Object
clone
,
equals
,
finalize
,
getClass
,
hashCode
,
notify
,
notifyAll
,
toString
,
wait
,
wait
,
wait
Methods inherited from interface org.springframework.data.mongodb.core.
MongoOperations
createView
,
createView
,
estimatedCount
,
exactCount
,
exactCount
,
findAndReplace
,
findAndReplace
,
findAndReplace
,
findAndReplace
,
findAndReplace
,
findAndReplace
,
findDistinct
,
findDistinct
,
withSession
MongoTemplate
Constructor used for a basic template configuration.
Parameters:
mongoClient
- must not be null.
databaseName
- must not be null or empty.
Since:
MongoTemplate
Constructor used for a basic template configuration.
Parameters:
mongoDbFactory
- must not be null.
public
MongoTemplate
(
MongoDatabaseFactory
mongoDbFactory,
@Nullable
MongoConverter
mongoConverter)
Constructor used for a basic template configuration.
Parameters:
mongoDbFactory
- must not be null.
mongoConverter
-
setWriteResultChecking
Configures the
WriteResultChecking
to be used with the template. Setting null will reset the
default of
DEFAULT_WRITE_RESULT_CHECKING
.
Parameters:
resultChecking
-
setWriteConcern
Configures the
WriteConcern
to be used with the template. If none is configured the
WriteConcern
configured on the
MongoDatabaseFactory
will apply.
Parameters:
writeConcern
-
setWriteConcernResolver
Configures the
WriteConcernResolver
to be used with the template.
Parameters:
writeConcernResolver
-
setReadPreference
Used by @{link
prepareCollection(MongoCollection)
to set the
ReadPreference
before any operations
are performed.
Parameters:
readPreference
-
setEntityLifecycleEventsEnabled
public
void
setEntityLifecycleEventsEnabled
(boolean enabled)
Configure whether lifecycle events such as
AfterLoadEvent
,
BeforeSaveEvent
, etc. should be
published or whether emission should be suppressed. Enabled by default.
Parameters:
enabled
-
true
to enable entity lifecycle events;
false
to disable entity lifecycle events.
Since:
See Also:
MongoMappingEvent
setApplicationContext
Specified by:
setApplicationContext
in interface
ApplicationContextAware
Throws:
BeansException
setEntityCallbacks
Set the
EntityCallbacks
instance to use when invoking
callbacks
like the
BeforeSaveCallback
.
Overrides potentially existing
EntityCallbacks
.
Parameters:
entityCallbacks
- must not be null.
Throws:
IllegalArgumentException
- if the given instance is null.
Since:
useEstimatedCount
public
void
useEstimatedCount
(boolean enabled)
Configure whether to use estimated count. Defaults to exact counting.
Parameters:
enabled
- use
MongoCollection.estimatedDocumentCount()
for unpaged and empty
queries
if
true
.
Since:
getConverter
Returns the default
MongoConverter
.
Specified by:
getConverter
in interface
MongoOperations
Returns:
Description copied from interface:
MongoOperations
Executes the given
Query
on the entity collection of the specified
entityType
backed by a Mongo DB
FindIterable
.
Returns a
String
that wraps the Mongo DB
FindIterable
that needs to be closed.
Specified by:
stream
in interface
MongoOperations
Type Parameters:
T
- element return type
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.
entityType
- must not be null.
Returns:
the result
Stream
, containing mapped objects, needing to be closed once fully processed (e.g.
through a try-with-resources clause).
Description copied from interface:
MongoOperations
Executes the given
Query
on the entity collection of the specified
entityType
and collection backed
by a Mongo DB
FindIterable
.
Returns a
Stream
that wraps the Mongo DB
FindIterable
that needs to be closed.
Specified by:
stream
in interface
MongoOperations
Type Parameters:
T
- element return type
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.
entityType
- must not be null.
collectionName
- must not be null or empty.
Returns:
the result
Stream
, containing mapped objects, needing to be closed once fully processed (e.g.
through a try-with-resources clause).
protected
<T>
Stream
<T>
doStream
(
Query
query,
Class
<?> entityType,
String
collectionName,
Class
<T> returnType)
getCollectionName
Description copied from interface:
MongoOperations
The collection name used for the specified class by this template.
Specified by:
getCollectionName
in interface
MongoOperations
Parameters:
entityClass
- must not be null.
Returns:
never null.
executeCommand
Description copied from interface:
MongoOperations
Execute a MongoDB command expressed as a JSON string. Parsing is delegated to
Document.parse(String)
to
obtain the
Document
holding the actual command. Any errors that result from executing this command will be
converted into Spring's DAO exception hierarchy.
Specified by:
executeCommand
in interface
MongoOperations
Parameters:
jsonCommand
- a MongoDB command expressed as a JSON string. Must not be null.
Returns:
a result object returned by the action.
executeCommand
public
org.bson.Document
executeCommand
(org.bson.Document command)
Description copied from interface:
MongoOperations
Execute a MongoDB command. Any errors that result from executing this command will be converted into Spring's DAO
exception hierarchy.
Specified by:
executeCommand
in interface
MongoOperations
Parameters:
command
- a MongoDB command.
Returns:
a result object returned by the action.
executeCommand
public
org.bson.Document
executeCommand
(org.bson.Document command,
@Nullable
com.mongodb.ReadPreference readPreference)
Description copied from interface:
MongoOperations
Execute a MongoDB command. Any errors that result from executing this command will be converted into Spring's data
access exception hierarchy.
Specified by:
executeCommand
in interface
MongoOperations
Parameters:
command
- a MongoDB command, must not be null.
readPreference
- read preferences to use, can be null.
Returns:
a result object returned by the action.
Description copied from interface:
MongoOperations
Execute a MongoDB query and iterate over the query results on a per-document basis with a DocumentCallbackHandler.
Specified by:
executeQuery
in interface
MongoOperations
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.
collectionName
- name of the collection to retrieve the objects from.
dch
- the handler that will extract results, one document at a time.
protected
void
executeQuery
(
Query
query,
String
collectionName,
DocumentCallbackHandler
documentCallbackHandler,
@Nullable
CursorPreparer
preparer)
Execute a MongoDB query and iterate over the query results on a per-document basis with a
DocumentCallbackHandler
using the provided CursorPreparer.
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification, must not be null.
collectionName
- name of the collection to retrieve the objects from
documentCallbackHandler
- the handler that will extract results, one document at a time
preparer
- allows for customization of the
FindIterable
used when iterating over the result set,
(apply limits, skips and so on).
execute
Description copied from interface:
MongoOperations
Executes a
DbCallback
translating any exceptions as necessary.
Allows for returning a result object, that is a domain object or a collection of domain objects.
Specified by:
execute
in interface
MongoOperations
Type Parameters:
T
- return type.
Parameters:
action
- callback object that specifies the MongoDB actions to perform on the passed in DB instance. Must not
be null.
Returns:
a result object returned by the action or null.
Description copied from interface:
MongoOperations
Executes the given
CollectionCallback
on the entity collection of the specified class.
Allows for returning a result object, that is a domain object or a collection of domain objects.
Specified by:
execute
in interface
MongoOperations
Type Parameters:
T
- return type.
Parameters:
entityClass
- class that determines the collection to use. Must not be null.
callback
- callback object that specifies the MongoDB action. Must not be null.
Returns:
a result object returned by the action or null.
Description copied from interface:
MongoOperations
Executes the given
CollectionCallback
on the collection of the given name.
Allows for returning a result object, that is a domain object or a collection of domain objects.
Specified by:
execute
in interface
MongoOperations
Type Parameters:
T
- return type.
Parameters:
collectionName
- the name of the collection that specifies which
MongoCollection
instance will be
passed into. Must not be null or empty.
callback
- callback object that specifies the MongoDB action the callback action. Must not be null.
Returns:
a result object returned by the action or null.
withSession
Description copied from interface:
MongoOperations
Obtain a
session
bound instance of
SessionScoped
binding a new
ClientSession
with given sessionOptions to each and every command issued against MongoDB.
Specified by:
withSession
in interface
MongoOperations
Parameters:
options
- must not be null.
Returns:
new instance of
SessionScoped
. Never null.
withSession
Description copied from interface:
MongoOperations
Obtain a
ClientSession
bound instance of
MongoOperations
.
Note:
It is up to the caller to manage the
ClientSession
lifecycle.
Specified by:
withSession
in interface
MongoOperations
Parameters:
session
- must not be null.
Returns:
ClientSession
bound instance of
MongoOperations
.
setSessionSynchronization
Define if
MongoTemplate
should participate in transactions. Default is set to
SessionSynchronization.ON_ACTUAL_TRANSACTION
.
NOTE:
MongoDB transactions require at least MongoDB 4.0.
Since:
createCollection
public
<T>
com.mongodb.client.MongoCollection<org.bson.Document>
createCollection
(
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Create an uncapped collection with a name based on the provided entity class.
Specified by:
createCollection
in interface
MongoOperations
Parameters:
entityClass
- class that determines the collection to create.
Returns:
the created collection.
createCollection
public
<T>
com.mongodb.client.MongoCollection<org.bson.Document>
createCollection
(
Class
<T> entityClass,
@Nullable
CollectionOptions
collectionOptions)
Description copied from interface:
MongoOperations
Create a collection with a name based on the provided entity class using the options.
Specified by:
createCollection
in interface
MongoOperations
Parameters:
entityClass
- class that determines the collection to create. Must not be null.
collectionOptions
- options to use when creating the collection.
Returns:
the created collection.
createCollection
public
com.mongodb.client.MongoCollection<org.bson.Document>
createCollection
(
String
collectionName)
Description copied from interface:
MongoOperations
Create an uncapped collection with the provided name.
Specified by:
createCollection
in interface
MongoOperations
Parameters:
collectionName
- name of the collection.
Returns:
the created collection.
createCollection
public
com.mongodb.client.MongoCollection<org.bson.Document>
createCollection
(
String
collectionName,
@Nullable
CollectionOptions
collectionOptions)
Description copied from interface:
MongoOperations
Create a collection with the provided name and options.
Specified by:
createCollection
in interface
MongoOperations
Parameters:
collectionName
- name of the collection. Must not be null nor empty.
collectionOptions
- options to use when creating the collection.
Returns:
the created collection.
createView
public
com.mongodb.client.MongoCollection<org.bson.Document>
createView
(
String
name,
Class
<?> source,
AggregationPipeline
pipeline,
@Nullable
ViewOptions
options)
Description copied from interface:
MongoOperations
Create a view with the provided name. The view content is defined by the
pipeline
on
another collection or view identified by the given
source type
.
Specified by:
createView
in interface
MongoOperations
Parameters:
name
- the name of the view to create.
source
- the type defining the views source collection.
pipeline
- the
AggregationPipeline
defining the view content.
options
- additional settings to apply when creating the view. Can be null.
createView
public
com.mongodb.client.MongoCollection<org.bson.Document>
createView
(
String
name,
String
source,
AggregationPipeline
pipeline,
@Nullable
ViewOptions
options)
Description copied from interface:
MongoOperations
Create a view with the provided name. The view content is defined by the
pipeline
on
another collection or view identified by the given source.
Specified by:
createView
in interface
MongoOperations
Parameters:
name
- the name of the view to create.
source
- the name of the collection or view defining the to be created views source.
pipeline
- the
AggregationPipeline
defining the view content.
options
- additional settings to apply when creating the view. Can be null.
doCreateView
protected
com.mongodb.client.MongoCollection<org.bson.Document>
doCreateView
(
String
name,
String
source,
List
<org.bson.Document> pipeline,
@Nullable
ViewOptions
options)
getCollection
Description copied from interface:
MongoOperations
Get a
MongoCollection
by its name. The returned collection may not exists yet (except in local memory) and
is created on first interaction with the server. Collections can be explicitly created via
MongoOperations.createCollection(Class)
. Please make sure to check if the collection
exists
first.
Translate any exceptions as necessary.
Specified by:
getCollection
in interface
MongoOperations
Parameters:
collectionName
- name of the collection. Must not be null.
Returns:
an existing collection or one created on first server interaction.
collectionExists
Description copied from interface:
MongoOperations
Check to see if a collection with a name indicated by the entity class exists.
Translate any exceptions as necessary.
Specified by:
collectionExists
in interface
MongoOperations
Parameters:
entityClass
- class that determines the name of the collection. Must not be null.
Returns:
true if a collection with the given name is found, false otherwise.
collectionExists
Description copied from interface:
MongoOperations
Check to see if a collection with a given name exists.
Translate any exceptions as necessary.
Specified by:
collectionExists
in interface
MongoOperations
Parameters:
collectionName
- name of the collection. Must not be null.
Returns:
true if a collection with the given name is found, false otherwise.
dropCollection
Description copied from interface:
MongoOperations
Drop the collection with the name indicated by the entity class.
Translate any exceptions as necessary.
Specified by:
dropCollection
in interface
MongoOperations
Parameters:
entityClass
- class that determines the collection to drop/delete. Must not be null.
dropCollection
Description copied from interface:
MongoOperations
Drop the collection with the given name.
Translate any exceptions as necessary.
Specified by:
dropCollection
in interface
MongoOperations
Parameters:
collectionName
- name of the collection to drop/delete.
indexOps
Description copied from interface:
MongoOperations
Returns the operations that can be performed on indexes
Specified by:
indexOps
in interface
IndexOperationsProvider
Specified by:
indexOps
in interface
MongoOperations
Parameters:
collectionName
- name of the MongoDB collection, must not be null.
Returns:
index operations on the named collection
Class
<?> type)
Description copied from interface:
IndexOperationsProvider
Returns the operations that can be performed on indexes.
Specified by:
indexOps
in interface
IndexOperationsProvider
Parameters:
collectionName
- name of the MongoDB collection, must not be null.
type
- the type used for field mapping. Can be null.
Returns:
index operations on the named collection
indexOps
Description copied from interface:
MongoOperations
Returns the operations that can be performed on indexes
Specified by:
indexOps
in interface
MongoOperations
Returns:
index operations on the named collection associated with the given entity class
bulkOps
Description copied from interface:
MongoOperations
Returns a new
BulkOperations
for the given collection.
NOTE:
Any additional support for field mapping, etc. is not available for update or
remove operations in bulk mode due to the lack of domain type information. Use
MongoOperations.bulkOps(BulkMode, Class, String)
to get full type specific support.
Specified by:
bulkOps
in interface
MongoOperations
Parameters:
bulkMode
- the
BulkOperations.BulkMode
to use for bulk operations, must not be null.
collectionName
- the name of the collection to work on, must not be null or empty.
Returns:
BulkOperations
on the named collection
bulkOps
Description copied from interface:
MongoOperations
Returns a new
BulkOperations
for the given entity type.
Specified by:
bulkOps
in interface
MongoOperations
Parameters:
bulkMode
- the
BulkOperations.BulkMode
to use for bulk operations, must not be null.
entityClass
- the name of the entity class, must not be null.
Returns:
BulkOperations
on the named collection associated of the given entity class.
public
BulkOperations
bulkOps
(
BulkOperations.BulkMode
mode,
@Nullable
Class
<?> entityType,
String
collectionName)
Description copied from interface:
MongoOperations
Returns a new
BulkOperations
for the given entity type and collection name.
Specified by:
bulkOps
in interface
MongoOperations
Parameters:
mode
- the
BulkOperations.BulkMode
to use for bulk operations, must not be null.
entityType
- the name of the entity class. Can be null.
collectionName
- the name of the collection to work on, must not be null or empty.
Returns:
BulkOperations
on the named collection associated with the given entity class.
scriptOps
Description copied from interface:
MongoOperations
Returns the
ScriptOperations
that can be performed on
MongoDatabase
level.
Specified by:
scriptOps
in interface
MongoOperations
Returns:
never null.
public
<T>
T
findOne
(
Query
query,
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Map the results of an ad-hoc query on the collection for the entity class to a single instance of an object of the
specified type.
The object is converted from the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
The query is specified as a
Query
which can be created either using the
BasicQuery
or the more
feature rich
Query
.
Specified by:
findOne
in interface
MongoOperations
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification.
entityClass
- the parametrized type of the returned list.
Returns:
the converted object.
Class
<T> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified
type.
The object is converted from the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
The query is specified as a
Query
which can be created either using the
BasicQuery
or the more
feature rich
Query
.
Specified by:
findOne
in interface
MongoOperations
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification.
entityClass
- the parametrized type of the returned list.
collectionName
- name of the collection to retrieve the objects from.
Returns:
the converted object.
Description copied from interface:
MongoOperations
Determine result of given
Query
contains at least one element.
Specified by:
exists
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the criteria used to find a record.
entityClass
- the parametrized type.
Returns:
true if the query yields a result.
Description copied from interface:
MongoOperations
Determine result of given
Query
contains at least one element.
NOTE:
Any additional support for query/field mapping, etc. is not available due to the lack of
domain type information. Use
MongoOperations.exists(Query, Class, String)
to get full type specific support.
Specified by:
exists
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the criteria used to find a record.
collectionName
- name of the collection to check for objects.
Returns:
true if the query yields a result.
Class
<?> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Determine result of given
Query
contains at least one element.
Specified by:
exists
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the criteria used to find a record.
entityClass
- the parametrized type. Can be null.
collectionName
- name of the collection to check for objects.
Returns:
true if the query yields a result.
Description copied from interface:
MongoOperations
Map the results of an ad-hoc query on the collection for the entity class to a List of the specified type.
The object is converted from the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
The query is specified as a
Query
which can be created either using the
BasicQuery
or the more
feature rich
Query
.
Specified by:
find
in interface
MongoOperations
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.
entityClass
- the parametrized type of the returned list. Must not be null.
Returns:
the List of converted objects.
Class
<T> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Map the results of an ad-hoc query on the specified collection to a List of the specified type.
The object is converted from the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
The query is specified as a
Query
which can be created either using the
BasicQuery
or the more
feature rich
Query
.
Specified by:
find
in interface
MongoOperations
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification. Must not be null.
entityClass
- the parametrized type of the returned list. Must not be null.
collectionName
- name of the collection to retrieve the objects from. Must not be null.
Returns:
the List of converted objects.
public
<T>
T
findById
(
Object
id,
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Returns a document with the given id mapped onto the given class. The collection the query is ran against will be
derived from the given target class as well.
Specified by:
findById
in interface
MongoOperations
Parameters:
id
- the id of the document to return. Must not be null.
entityClass
- the type the document shall be converted into. Must not be null.
Returns:
the document with the given id mapped onto the given target class.
Class
<T> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Returns the document with the given id from the given collection mapped onto the given target class.
Specified by:
findById
in interface
MongoOperations
Parameters:
id
- the id of the document to return.
entityClass
- the type to convert the document to.
collectionName
- the collection to query for the document.
Returns:
he converted object or null if document does not exist.
Class
<?> entityClass,
Class
<T> resultClass)
Description copied from interface:
MongoOperations
Finds the distinct values for a specified field across a single
MongoCollection
or view and
returns the results in a
List
.
Specified by:
findDistinct
in interface
MongoOperations
Parameters:
query
- filter
Query
to restrict search. Must not be null.
field
- the name of the field to inspect for distinct values. Must not be null.
entityClass
- the domain type used for determining the actual
MongoCollection
and mapping the
Query
to the domain type fields. Must not be null.
resultClass
- the result type. Must not be null.
Returns:
never null.
Class
<?> entityClass,
Class
<T> resultClass)
Description copied from interface:
MongoOperations
Finds the distinct values for a specified field across a single
MongoCollection
or view and
returns the results in a
List
.
Specified by:
findDistinct
in interface
MongoOperations
Parameters:
query
- filter
Query
to restrict search. Must not be null.
field
- the name of the field to inspect for distinct values. Must not be null.
collectionName
- the explicit name of the actual
MongoCollection
. Must not be null.
entityClass
- the domain type used for mapping the
Query
to the domain type fields.
resultClass
- the result type. Must not be null.
Returns:
never null.
Description copied from interface:
MongoOperations
Returns
GeoResults
for all entities matching the given
NearQuery
. Will consider entity mapping
information to determine the collection the query is ran against. Note, that MongoDB limits the number of results
by default. Make sure to add an explicit limit to the
NearQuery
if you expect a particular number of
results.
MongoDB 4.2 has removed the
geoNear
command. This method uses since version 2.2 aggregations and the
$geoNear
aggregation command to emulate
geoNear
command functionality. We recommend using
aggregations directly:
TypedAggregation<T> geoNear = TypedAggregation.newAggregation(entityClass, Aggregation.geoNear(near, "dis"))
.withOptions(AggregationOptions.builder().collation(near.getCollation()).build());
AggregationResults<Document> results = aggregate(geoNear, Document.class);
Specified by:
geoNear
in interface MongoOperations
Parameters:
near
- must not be null.
entityClass
- must not be null.
Returns:
Description copied from interface: MongoOperations
Returns GeoResults
for all entities matching the given NearQuery
. Note, that MongoDB limits the
number of results by default. Make sure to add an explicit limit to the NearQuery
if you expect a
particular number of results.
MongoDB 4.2 has removed the geoNear
command. This method uses since version 2.2 aggregations and the
$geoNear
aggregation command to emulate geoNear
command functionality. We recommend using
aggregations directly:
TypedAggregation<T> geoNear = TypedAggregation.newAggregation(entityClass, Aggregation.geoNear(near, "dis"))
.withOptions(AggregationOptions.builder().collation(near.getCollation()).build());
AggregationResults<Document> results = aggregate(geoNear, Document.class);
Specified by:
geoNear
in interface MongoOperations
Parameters:
near
- must not be null.
domainType
- must not be null.
collectionName
- the collection to trigger the query against. If no collection name is given the entity class
will be inspected. Must not be null nor empty.
Returns:
public <T> GeoResults<T> geoNear (NearQuery near,
Class<?> domainType,
String collectionName,
Class<T> returnType)
public <T> T findAndModify (Query query,
UpdateDefinition update,
Class<T> entityClass)
Description copied from interface: MongoOperations
Specified by:
findAndModify
in interface MongoOperations
Parameters:
query
- the Query
class that specifies the Criteria
used to find a record and also an optional
fields specification. Must not be null.
update
- the UpdateDefinition
to apply on matching documents. Must not be null.
entityClass
- the parametrized type. Must not be null.
Returns:
the converted object that was updated before it was updated or null, if not found.
See Also:
Update
AggregationUpdate
public <T> T findAndModify (Query query,
UpdateDefinition update,
Class<T> entityClass,
String collectionName)
Description copied from interface:
MongoOperations
Specified by:
findAndModify
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the
Criteria
used to find a record and also an optional
fields specification. Must not be null.
update
- the
UpdateDefinition
to apply on matching documents. Must not be null.
entityClass
- the parametrized type. Must not be null.
collectionName
- the collection to query. Must not be null.
Returns:
the converted object that was updated before it was updated or null, if not found.
See Also:
Update
AggregationUpdate
public
<T>
T
findAndModify
(
Query
query,
UpdateDefinition
update,
FindAndModifyOptions
options,
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Triggers
findAndModify
to apply provided
Update
on documents matching
Criteria
of given
Query
taking
FindAndModifyOptions
into account.
Specified by:
findAndModify
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the
Criteria
used to find a record and also an optional
fields specification.
update
- the
UpdateDefinition
to apply on matching documents.
options
- the
FindAndModifyOptions
holding additional information.
entityClass
- the parametrized type.
Returns:
the converted object that was updated or null, if not found. Depending on the value of
FindAndModifyOptions.isReturnNew()
this will either be the object as it was before the update or as
it is after the update.
See Also:
Update
AggregationUpdate
public
<T>
T
findAndModify
(
Query
query,
UpdateDefinition
update,
FindAndModifyOptions
options,
Class
<T> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Triggers
findAndModify
to apply provided
Update
on documents matching
Criteria
of given
Query
taking
FindAndModifyOptions
into account.
Specified by:
findAndModify
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the
Criteria
used to find a record and also an optional
fields specification. Must not be null.
update
- the
UpdateDefinition
to apply on matching documents. Must not be null.
options
- the
FindAndModifyOptions
holding additional information. Must not be null.
entityClass
- the parametrized type. Must not be null.
collectionName
- the collection to query. Must not be null.
Returns:
the converted object that was updated or null, if not found. Depending on the value of
FindAndModifyOptions.isReturnNew()
this will either be the object as it was before the update or as
it is after the update.
See Also:
Update
AggregationUpdate
String
collectionName,
Class
<T> resultType)
Description copied from interface:
MongoOperations
Triggers
findOneAndReplace
to replace a single document matching
Criteria
of given
Query
with the
replacement
document
taking
FindAndReplaceOptions
into account.
NOTE:
The replacement entity must not hold an id.
Specified by:
findAndReplace
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the
Criteria
used to find a record and also an optional
fields specification. Must not be null.
replacement
- the replacement document. Must not be null.
options
- the
FindAndModifyOptions
holding additional information. Must not be null.
entityType
- the type used for mapping the
Query
to domain type fields. Must not be null.
collectionName
- the collection to query. Must not be null.
resultType
- the parametrized type projection return type. Must not be null, use the domain type of
Object.class
instead.
Returns:
the converted object that was updated or null, if not found. Depending on the value of
FindAndReplaceOptions.isReturnNew()
this will either be the object as it was before the update or
as it is after the update.
public
<T>
T
findAndRemove
(
Query
query,
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Map the results of an ad-hoc query on the collection for the entity type to a single instance of an object of the
specified type. The first document that matches the query is returned and also removed from the collection in the
database.
The object is converted from the MongoDB native representation using an instance of .
The query is specified as a
Query
which can be created either using the
BasicQuery
or the more
feature rich
Query
.
Specified by:
findAndRemove
in interface
MongoOperations
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification.
entityClass
- the parametrized type of the returned list.
Returns:
the converted object
public
<T>
T
findAndRemove
(
Query
query,
Class
<T> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified
type. The first document that matches the query is returned and also removed from the collection in the database.
The object is converted from the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
The query is specified as a
Query
which can be created either using the
BasicQuery
or the more
feature rich
Query
.
Specified by:
findAndRemove
in interface
MongoOperations
Parameters:
query
- the query class that specifies the criteria used to find a record and also an optional fields
specification.
entityClass
- the parametrized type of the returned list.
collectionName
- name of the collection to retrieve the objects from.
Returns:
the converted object.
Description copied from interface:
MongoOperations
Returns the number of documents for the given
Query
by querying the collection of the given entity class.
NOTE:
Query
offset
and
limit
can have direct
influence on the resulting number of documents found as those values are passed on to the server and potentially
limit the range and order within which the server performs the count operation. Use an unpaged query to
count all matches.
This method may choose to use
MongoOperations.estimatedCount(Class)
for empty queries instead of running an
aggregation execution
which may have an impact on performance.
Specified by:
count
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the criteria used to find documents. Must not be
null.
entityClass
- class that determines the collection to use. Must not be null.
Returns:
the count of matching documents.
See Also:
MongoOperations.exactCount(Query, Class)
MongoOperations.estimatedCount(Class)
Description copied from interface:
MongoOperations
Returns the number of documents for the given
Query
querying the given collection. The given
Query
must solely consist of document field references as we lack type information to map potential property references
onto document fields. Use
MongoOperations.count(Query, Class, String)
to get full type specific support.
NOTE:
Query
offset
and
limit
can have direct
influence on the resulting number of documents found as those values are passed on to the server and potentially
limit the range and order within which the server performs the count operation. Use an unpaged query to
count all matches.
This method may choose to use
MongoOperations.estimatedCount(Class)
for empty queries instead of running an
aggregation execution
which may have an impact on performance.
Specified by:
count
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the criteria used to find documents.
collectionName
- must not be null or empty.
Returns:
the count of matching documents.
See Also:
MongoOperations.count(Query, Class, String)
MongoOperations.exactCount(Query, String)
MongoOperations.estimatedCount(String)
Class
<?> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Returns the number of documents for the given
Query
by querying the given collection using the given entity
class to map the given
Query
.
NOTE:
Query
offset
and
limit
can have direct
influence on the resulting number of documents found as those values are passed on to the server and potentially
limit the range and order within which the server performs the count operation. Use an unpaged query to
count all matches.
This method may choose to use
MongoOperations.estimatedCount(Class)
for empty queries instead of running an
aggregation execution
which may have an impact on performance.
Specified by:
count
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the criteria used to find documents. Must not be
null.
entityClass
- the parametrized type. Can be null.
collectionName
- must not be null or empty.
Returns:
the count of matching documents.
See Also:
MongoOperations.count(Query, Class, String)
MongoOperations.estimatedCount(String)
estimatedCount
Description copied from interface:
MongoOperations
Estimate the number of documents in the given collection based on collection statistics.
Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
transactions.
Specified by:
estimatedCount
in interface
MongoOperations
Parameters:
collectionName
- must not be null.
Returns:
the estimated number of documents.
Class
<?> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Returns the number of documents for the given
Query
by querying the given collection using the given entity
class to map the given
Query
.
NOTE:
Query
offset
and
limit
can have direct
influence on the resulting number of documents found as those values are passed on to the server and potentially
limit the range and order within which the server performs the count operation. Use an unpaged query to
count all matches.
This method uses an
aggregation execution
even for empty
queries
which may have an impact on performance, but guarantees
shard, session and transaction compliance. In case an inaccurate count satisfies the applications needs use
MongoOperations.estimatedCount(String)
for empty queries instead.
Specified by:
exactCount
in interface
MongoOperations
Parameters:
query
- the
Query
class that specifies the criteria used to find documents. Must not be
null.
entityClass
- the parametrized type. Can be null.
collectionName
- must not be null or empty.
Returns:
the count of matching documents.
doExactCount
protected
long
doExactCount
(
String
collectionName,
org.bson.Document filter,
com.mongodb.client.model.CountOptions options)
insert
public
<T>
T
insert
(T objectToSave)
Description copied from interface:
MongoOperations
Insert the object into the collection for the entity type of the object to save.
The object is converted to the MongoDB native representation using an instance of .
If your object has an Id property which holds a null value, it will be set with the generated
Id from MongoDB. If your Id property is a String then MongoDB ObjectId will be used to populate that string.
Otherwise, the conversion from ObjectId to your property type will be handled by Spring's BeanWrapper class that
leverages Type Conversion API. See
Spring's
Type Conversion"
for more details.
Insert is used to initially store the object into the database. To update an existing object use the save method.
The
objectToSave
must not be collection-like.
Specified by:
insert
in interface
MongoOperations
Parameters:
objectToSave
- the object to store in the collection. Must not be null.
Returns:
the inserted object.
Description copied from interface:
MongoOperations
Insert the object into the specified collection.
The object is converted to the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
Insert is used to initially store the object into the database. To update an existing object use the save method.
The
objectToSave
must not be collection-like.
Specified by:
insert
in interface
MongoOperations
Parameters:
objectToSave
- the object to store in the collection. Must not be null.
collectionName
- name of the collection to store the object in. Must not be null.
Returns:
the inserted object.
ensureNotCollectionLike
Parameters:
source
- can be null.
Since:
prepareCollection
protected
com.mongodb.client.MongoCollection<org.bson.Document>
prepareCollection
(com.mongodb.client.MongoCollection<org.bson.Document> collection)
Prepare the collection before any processing is done using it. This allows a convenient way to apply settings like
withCodecRegistry() etc. Can be overridden in sub-classes.
Parameters:
collection
-
prepareWriteConcern
Prepare the WriteConcern before any processing is done using it. This allows a convenient way to apply custom
settings in sub-classes.
In case of using MongoDB Java driver version 3 the returned
WriteConcern
will be defaulted to
WriteConcern.ACKNOWLEDGED
when
WriteResultChecking
is set to
WriteResultChecking.EXCEPTION
.
Parameters:
mongoAction
- any MongoAction already configured or null
Returns:
The prepared WriteConcern or null
insert
Description copied from interface:
MongoOperations
Insert a Collection of objects into a collection in a single batch write to the database.
Specified by:
insert
in interface
MongoOperations
Parameters:
batchToSave
- the batch of objects to save. Must not be null.
entityClass
- class that determines the collection to use. Must not be null.
Returns:
the inserted objects that.
insert
Description copied from interface:
MongoOperations
Insert a batch of objects into the specified collection in a single batch write to the database.
Specified by:
insert
in interface
MongoOperations
Parameters:
batchToSave
- the list of objects to save. Must not be null.
collectionName
- name of the collection to store the object in. Must not be null.
Returns:
the inserted objects that.
insertAll
Description copied from interface:
MongoOperations
Insert a mixed Collection of objects into a database collection determining the collection name to use based on the
class.
Specified by:
insertAll
in interface
MongoOperations
Parameters:
objectsToSave
- the list of objects to save. Must not be null.
Returns:
the inserted objects.
doInsertAll
protected
<T>
Collection
<T>
doInsertAll
(
Collection
<? extends T> listToSave,
MongoWriter
<T> writer)
doInsertBatch
protected
<T>
Collection
<T>
doInsertBatch
(
String
collectionName,
Collection
<? extends T> batchToSave,
MongoWriter
<T> writer)
public
<T>
T
save
(T objectToSave)
Description copied from interface:
MongoOperations
Save the object to the collection for the entity type of the object to save. This will perform an insert if the
object is not already present, that is an 'upsert'.
The object is converted to the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
Spring's
Type Conversion"
for more details.
The
objectToSave
must not be collection-like.
Specified by:
save
in interface
MongoOperations
Parameters:
objectToSave
- the object to store in the collection. Must not be null.
Returns:
the saved object.
Description copied from interface:
MongoOperations
Save the object to the specified collection. This will perform an insert if the object is not already present, that
is an 'upsert'.
The object is converted to the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
Spring's Type
Conversion
for more details.
The
objectToSave
must not be collection-like.
Specified by:
save
in interface
MongoOperations
Parameters:
objectToSave
- the object to store in the collection. Must not be null.
collectionName
- name of the collection to store the object in. Must not be null.
Returns:
the saved object.
insertDocument
protected
Object
insertDocument
(
String
collectionName,
org.bson.Document document,
Class
<?> entityClass)
saveDocument
protected
Object
saveDocument
(
String
collectionName,
org.bson.Document dbDoc,
Class
<?> entityClass)
upsert
public
com.mongodb.client.result.UpdateResult
upsert
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass)
Description copied from interface:
MongoOperations
Performs an upsert. If no document is found that matches the query, a new document is created and inserted by
combining the query document and the update document.
NOTE:
sorting
is not supported by
db.collection.updateOne
.
Use
MongoOperations.findAndModify(Query, UpdateDefinition, FindAndModifyOptions, Class, String)
instead.
Specified by:
upsert
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be upserted. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing object. Must not be null.
entityClass
- class that determines the collection to use. Must not be null.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
upsert
public
com.mongodb.client.result.UpdateResult
upsert
(
Query
query,
UpdateDefinition
update,
String
collectionName)
Description copied from interface:
MongoOperations
Performs an upsert. If no document is found that matches the query, a new document is created and inserted by
combining the query document and the update document.
NOTE:
Any additional support for field mapping, versions, etc. is not available due to the lack of
domain type information. Use
MongoOperations.upsert(Query, UpdateDefinition, Class, String)
to get full type specific
support.
NOTE:
sorting
is not supported by
db.collection.updateOne
.
Use
MongoOperations.findAndModify(Query, UpdateDefinition, FindAndModifyOptions, Class, String)
instead.
Specified by:
upsert
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be upserted. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing object. Must not be null.
collectionName
- name of the collection to update the object in.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
upsert
public
com.mongodb.client.result.UpdateResult
upsert
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Performs an upsert. If no document is found that matches the query, a new document is created and inserted by
combining the query document and the update document.
Specified by:
upsert
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be upserted. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing object. Must not be null.
entityClass
- class of the pojo to be operated on. Must not be null.
collectionName
- name of the collection to update the object in. Must not be null.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
updateFirst
public
com.mongodb.client.result.UpdateResult
updateFirst
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass)
Description copied from interface:
MongoOperations
Updates the first object that is found in the collection of the entity class that matches the query document with
the provided update document.
Specified by:
updateFirst
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be updated. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing. Must not be null.
entityClass
- class that determines the collection to use.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
updateFirst
public
com.mongodb.client.result.UpdateResult
updateFirst
(
Query
query,
UpdateDefinition
update,
String
collectionName)
Description copied from interface:
MongoOperations
Updates the first object that is found in the specified collection that matches the query document criteria with
the provided updated document.
NOTE:
Any additional support for field mapping, versions, etc. is not available due to the lack of
domain type information. Use
MongoOperations.updateFirst(Query, UpdateDefinition, Class, String)
to get full type specific
support.
NOTE:
sorting
is not supported by
db.collection.updateOne
.
Use
MongoOperations.findAndModify(Query, UpdateDefinition, Class, String)
instead.
Specified by:
updateFirst
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be updated. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing. Must not be null.
collectionName
- name of the collection to update the object in. Must not be null.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
updateFirst
public
com.mongodb.client.result.UpdateResult
updateFirst
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Updates the first object that is found in the specified collection that matches the query document criteria with
the provided updated document.
Specified by:
updateFirst
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be updated. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing. Must not be null.
entityClass
- class of the pojo to be operated on. Must not be null.
collectionName
- name of the collection to update the object in. Must not be null.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
updateMulti
public
com.mongodb.client.result.UpdateResult
updateMulti
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass)
Description copied from interface:
MongoOperations
Updates all objects that are found in the collection for the entity class that matches the query document criteria
with the provided updated document.
Specified by:
updateMulti
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be updated. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing. Must not be null.
entityClass
- class of the pojo to be operated on. Must not be null.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
updateMulti
public
com.mongodb.client.result.UpdateResult
updateMulti
(
Query
query,
UpdateDefinition
update,
String
collectionName)
Description copied from interface:
MongoOperations
Updates all objects that are found in the specified collection that matches the query document criteria with the
provided updated document.
NOTE:
Any additional support for field mapping, versions, etc. is not available due to the lack of
domain type information. Use
MongoOperations.updateMulti(Query, UpdateDefinition, Class, String)
to get full type specific
support.
Specified by:
updateMulti
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be updated. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing. Must not be null.
collectionName
- name of the collection to update the object in. Must not be null.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
updateMulti
public
com.mongodb.client.result.UpdateResult
updateMulti
(
Query
query,
UpdateDefinition
update,
Class
<?> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Updates all objects that are found in the collection for the entity class that matches the query document criteria
with the provided updated document.
Specified by:
updateMulti
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to select a record to be updated. Must not be
null.
update
- the
UpdateDefinition
that contains the updated object or
$
operators to manipulate
the existing. Must not be null.
entityClass
- class of the pojo to be operated on. Must not be null.
collectionName
- name of the collection to update the object in. Must not be null.
Returns:
the
UpdateResult
which lets you access the results of the previous write.
See Also:
Update
AggregationUpdate
doUpdate
protected
com.mongodb.client.result.UpdateResult
doUpdate
(
String
collectionName,
Query
query,
UpdateDefinition
update,
@Nullable
Class
<?> entityClass,
boolean upsert,
boolean multi)
remove
Description copied from interface:
MongoOperations
Remove the given object from the collection by id and (if applicable) its
Version
.
Use
DeleteResult.getDeletedCount()
for insight whether an
acknowledged
remove operation was successful or not.
Specified by:
remove
in interface
MongoOperations
Parameters:
object
- must not be null.
Returns:
the
DeleteResult
which lets you access the results of the previous delete.
remove
Description copied from interface:
MongoOperations
Removes the given object from the given collection by id and (if applicable) its
Version
.
Use
DeleteResult.getDeletedCount()
for insight whether an
acknowledged
remove operation was successful or not.
Specified by:
remove
in interface
MongoOperations
Parameters:
object
- must not be null.
collectionName
- name of the collection where the objects will removed, must not be null or empty.
Returns:
the
DeleteResult
which lets you access the results of the previous delete.
remove
Description copied from interface:
MongoOperations
Remove all documents from the specified collection that match the provided query document criteria. There is no
conversion/mapping done for any criteria using the id field.
NOTE:
Any additional support for field mapping is not available due to the lack of domain type
information. Use
MongoOperations.remove(Query, Class, String)
to get full type specific support.
Specified by:
remove
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to remove a record.
collectionName
- name of the collection where the objects will removed, must not be null or empty.
Returns:
the
DeleteResult
which lets you access the results of the previous delete.
remove
Description copied from interface:
MongoOperations
Remove all documents that match the provided query document criteria from the collection used to store the
entityClass. The Class parameter is also used to help convert the Id of the object if it is present in the query.
Specified by:
remove
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to remove a record.
entityClass
- class that determines the collection to use.
Returns:
the
DeleteResult
which lets you access the results of the previous delete.
remove
public
com.mongodb.client.result.DeleteResult
remove
(
Query
query,
Class
<?> entityClass,
String
collectionName)
Description copied from interface:
MongoOperations
Remove all documents that match the provided query document criteria from the collection used to store the
entityClass. The Class parameter is also used to help convert the Id of the object if it is present in the query.
Specified by:
remove
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to remove a record.
entityClass
- class of the pojo to be operated on. Can be null.
collectionName
- name of the collection where the objects will removed, must not be null or empty.
Returns:
the
DeleteResult
which lets you access the results of the previous delete.
doRemove
protected
<T>
com.mongodb.client.result.DeleteResult
doRemove
(
String
collectionName,
Query
query,
@Nullable
Class
<T> entityClass,
boolean multi)
findAll
Description copied from interface:
MongoOperations
Query for a list of objects of type T from the collection used by the entity class.
The object is converted from the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way
to map objects since the test for class type is done in the client and not on the server.
Specified by:
findAll
in interface
MongoOperations
Parameters:
entityClass
- the parametrized type of the returned list.
Returns:
the converted collection.
Description copied from interface:
MongoOperations
Query for a list of objects of type T from the specified collection.
The object is converted from the MongoDB native representation using an instance of . Unless
configured otherwise, an instance of
MappingMongoConverter
will be used.
If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way
to map objects since the test for class type is done in the client and not on the server.
Specified by:
findAll
in interface
MongoOperations
Parameters:
entityClass
- the parametrized type of the returned list.
collectionName
- name of the collection to retrieve the objects from.
Returns:
the converted collection.
mapReduce
public
<T>
MapReduceResults
<T>
mapReduce
(
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Execute a map-reduce operation. The map-reduce operation will be formed with an output type of INLINE
Specified by:
mapReduce
in interface
MongoOperations
Parameters:
inputCollectionName
- the collection where the map-reduce will read from. Must not be null.
mapFunction
- The JavaScript map function.
reduceFunction
- The JavaScript reduce function
entityClass
- The parametrized type of the returned list. Must not be null.
Returns:
The results of the map reduce operation
mapReduce
public
<T>
MapReduceResults
<T>
mapReduce
(
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
@Nullable
MapReduceOptions
mapReduceOptions,
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Execute a map-reduce operation that takes additional map-reduce options.
Specified by:
mapReduce
in interface
MongoOperations
Parameters:
inputCollectionName
- the collection where the map-reduce will read from. Must not be null.
mapFunction
- The JavaScript map function
reduceFunction
- The JavaScript reduce function
mapReduceOptions
- Options that specify detailed map-reduce behavior.
entityClass
- The parametrized type of the returned list. Must not be null.
Returns:
The results of the map reduce operation
mapReduce
public
<T>
MapReduceResults
<T>
mapReduce
(
Query
query,
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Execute a map-reduce operation that takes a query. The map-reduce operation will be formed with an output type of
INLINE
Specified by:
mapReduce
in interface
MongoOperations
Parameters:
query
- The query to use to select the data for the map phase. Must not be null.
inputCollectionName
- the collection where the map-reduce will read from. Must not be null.
mapFunction
- The JavaScript map function
reduceFunction
- The JavaScript reduce function
entityClass
- The parametrized type of the returned list. Must not be null.
Returns:
The results of the map reduce operation
mapReduce
public
<T>
MapReduceResults
<T>
mapReduce
(
Query
query,
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
@Nullable
MapReduceOptions
mapReduceOptions,
Class
<T> entityClass)
Description copied from interface:
MongoOperations
Execute a map-reduce operation that takes a query and additional map-reduce options
Specified by:
mapReduce
in interface
MongoOperations
Parameters:
query
- The query to use to select the data for the map phase. Must not be null.
inputCollectionName
- the collection where the map-reduce will read from. Must not be null.
mapFunction
- The JavaScript map function
reduceFunction
- The JavaScript reduce function
mapReduceOptions
- Options that specify detailed map-reduce behavior
entityClass
- The parametrized type of the returned list. Must not be null.
Returns:
The results of the map reduce operation
public
<T>
List
<T>
mapReduce
(
Query
query,
Class
<?> domainType,
String
inputCollectionName,
String
mapFunction,
String
reduceFunction,
@Nullable
MapReduceOptions
mapReduceOptions,
Class
<T> resultType)
Deprecated.
since 3.4 in favor of
aggregate(TypedAggregation, Class)
.
Parameters:
query
-
domainType
-
inputCollectionName
-
mapFunction
-
reduceFunction
-
mapReduceOptions
-
resultType
-
Returns:
Since:
aggregate
public
<O>
AggregationResults
<O>
aggregate
(
TypedAggregation
<?> aggregation,
Class
<O> outputType)
Description copied from interface:
MongoOperations
Execute an aggregation operation. The raw results will be mapped to the given entity class. The name of the
inputCollection is derived from the inputType of the aggregation.
Specified by:
aggregate
in interface
MongoOperations
Parameters:
aggregation
- The
TypedAggregation
specification holding the aggregation operations, must not be
null.
outputType
- The parametrized type of the returned list, must not be null.
Returns:
The results of the aggregation operation.
aggregate
public
<O>
AggregationResults
<O>
aggregate
(
TypedAggregation
<?> aggregation,
String
inputCollectionName,
Class
<O> outputType)
Description copied from interface:
MongoOperations
Execute an aggregation operation. The raw results will be mapped to the given entity class. The name of the
inputCollection is derived from the inputType of the aggregation.
Specified by:
aggregate
in interface
MongoOperations
Parameters:
aggregation
- The
TypedAggregation
specification holding the aggregation operations, must not be
null.
inputCollectionName
- The name of the input collection to use for the aggreation.
outputType
- The parametrized type of the returned list, must not be null.
Returns:
The results of the aggregation operation.
aggregate
public
<O>
AggregationResults
<O>
aggregate
(
Aggregation
aggregation,
Class
<?> inputType,
Class
<O> outputType)
Description copied from interface:
MongoOperations
Execute an aggregation operation. The raw results will be mapped to the given entity class.
Specified by:
aggregate
in interface
MongoOperations
Parameters:
aggregation
- The
Aggregation
specification holding the aggregation operations, must not be
null.
inputType
- the inputType where the aggregation operation will read from, must not be null or
empty.
outputType
- The parametrized type of the returned list, must not be null.
Returns:
The results of the aggregation operation.
aggregate
public
<O>
AggregationResults
<O>
aggregate
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType)
Description copied from interface:
MongoOperations
Execute an aggregation operation. The raw results will be mapped to the given entity class.
Specified by:
aggregate
in interface
MongoOperations
Parameters:
aggregation
- The
Aggregation
specification holding the aggregation operations, must not be
null.
collectionName
- the collection where the aggregation operation will read from, must not be null or
empty.
outputType
- The parametrized type of the returned list, must not be null.
Returns:
The results of the aggregation operation.
aggregateStream
public
<O>
Stream
<O>
aggregateStream
(
TypedAggregation
<?> aggregation,
String
inputCollectionName,
Class
<O> outputType)
Description copied from interface:
MongoOperations
Execute an aggregation operation backed by a Mongo DB
AggregateIterable
.
Returns a
Stream
that wraps the Mongo DB
AggregateIterable
that needs to be
closed. The raw results will be mapped to the given entity class. The name of the inputCollection is derived from
the inputType of the aggregation.
Aggregation streaming can't be used with
aggregation explain
. Enabling
explanation mode will throw an
IllegalArgumentException
.
Specified by:
aggregateStream
in interface
MongoOperations
Parameters:
aggregation
- The
TypedAggregation
specification holding the aggregation operations, must not be
null.
inputCollectionName
- The name of the input collection to use for the aggreation.
outputType
- The parametrized type of the returned list, must not be null.
Returns:
the result
Stream
, containing mapped objects, needing to be closed once fully processed (e.g.
through a try-with-resources clause).
aggregateStream
Description copied from interface:
MongoOperations
Execute an aggregation operation backed by a Mongo DB
AggregateIterable
.
Returns a
Stream
that wraps the Mongo DB
AggregateIterable
that needs to be
closed. The raw results will be mapped to the given entity class and are returned as stream. The name of the
inputCollection is derived from the inputType of the aggregation.
Aggregation streaming can't be used with
aggregation explain
. Enabling
explanation mode will throw an
IllegalArgumentException
.
Specified by:
aggregateStream
in interface
MongoOperations
Parameters:
aggregation
- The
TypedAggregation
specification holding the aggregation operations, must not be
null.
outputType
- The parametrized type of the returned list, must not be null.
Returns:
the result
Stream
, containing mapped objects, needing to be closed once fully processed (e.g.
through a try-with-resources clause).
aggregateStream
public
<O>
Stream
<O>
aggregateStream
(
Aggregation
aggregation,
Class
<?> inputType,
Class
<O> outputType)
Description copied from interface:
MongoOperations
Execute an aggregation operation backed by a Mongo DB
AggregateIterable
.
Returns a
Stream
that wraps the Mongo DB
AggregateIterable
that needs to be
closed. The raw results will be mapped to the given entity class.
Aggregation streaming can't be used with
aggregation explain
. Enabling
explanation mode will throw an
IllegalArgumentException
.
Specified by:
aggregateStream
in interface
MongoOperations
Parameters:
aggregation
- The
Aggregation
specification holding the aggregation operations, must not be
null.
inputType
- the inputType where the aggregation operation will read from, must not be null or
empty.
outputType
- The parametrized type of the returned list, must not be null.
Returns:
the result
Stream
, containing mapped objects, needing to be closed once fully processed (e.g.
through a try-with-resources clause).
aggregateStream
public
<O>
Stream
<O>
aggregateStream
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType)
Description copied from interface:
MongoOperations
Execute an aggregation operation backed by a Mongo DB
AggregateIterable
.
Returns a
Stream
that wraps the Mongo DB
AggregateIterable
that needs to be
closed. The raw results will be mapped to the given entity class.
Aggregation streaming can't be used with
aggregation explain
. Enabling
explanation mode will throw an
IllegalArgumentException
.
Specified by:
aggregateStream
in interface
MongoOperations
Parameters:
aggregation
- The
Aggregation
specification holding the aggregation operations, must not be
null.
collectionName
- the collection where the aggregation operation will read from, must not be null or
empty.
outputType
- The parametrized type of the returned list, must not be null.
Returns:
the result
Stream
, containing mapped objects, needing to be closed once fully processed (e.g.
through a try-with-resources clause).
findAllAndRemove
Description copied from interface:
MongoOperations
Returns and removes all documents form the specified collection that match the provided query.
NOTE:
Any additional support for field mapping is not available due to the lack of domain type
information. Use
MongoOperations.findAllAndRemove(Query, Class, String)
to get full type specific support.
Specified by:
findAllAndRemove
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to find and remove documents.
collectionName
- name of the collection where the objects will removed, must not be null or empty.
Returns:
the
List
converted objects deleted by this operation.
findAllAndRemove
Description copied from interface:
MongoOperations
Returns and removes all documents matching the given query form the collection used to store the entityClass.
Specified by:
findAllAndRemove
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to find and remove documents.
entityClass
- class of the pojo to be operated on.
Returns:
the
List
converted objects deleted by this operation.
findAllAndRemove
Description copied from interface:
MongoOperations
Returns and removes all documents that match the provided query document criteria from the collection used to store
the entityClass. The Class parameter is also used to help convert the Id of the object if it is present in the
query.
Specified by:
findAllAndRemove
in interface
MongoOperations
Parameters:
query
- the query document that specifies the criteria used to find and remove documents.
entityClass
- class of the pojo to be operated on.
collectionName
- name of the collection where the objects will removed, must not be null or empty.
Returns:
the
List
converted objects deleted by this operation.
doFindAndDelete
Retrieve and remove all documents matching the given
query
by calling
find(Query, Class, String)
and
remove(Query, Class, String)
, whereas the
Query
for
remove(Query, Class, String)
is
constructed out of the find result.
Parameters:
collectionName
-
query
-
entityClass
-
Returns:
aggregate
protected
<O>
AggregationResults
<O>
aggregate
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType,
@Nullable
AggregationOperationContext
context)
doAggregate
protected
<O>
AggregationResults
<O>
doAggregate
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType,
AggregationOperationContext
context)
aggregateStream
protected
<O>
Stream
<O>
aggregateStream
(
Aggregation
aggregation,
String
collectionName,
Class
<O> outputType,
@Nullable
AggregationOperationContext
context)
Description copied from interface:
ExecutableFindOperation
Start creating a find operation for the given domainType.
Specified by:
query
in interface
ExecutableFindOperation
Parameters:
domainType
- must not be null.
Returns:
new instance of
ExecutableFindOperation.ExecutableFind
.
update
Description copied from interface:
ExecutableUpdateOperation
Start creating an update operation for the given domainType.
Specified by:
update
in interface
ExecutableUpdateOperation
Parameters:
domainType
- must not be null.
Returns:
new instance of
ExecutableUpdateOperation.ExecutableUpdate
.
remove
Description copied from interface:
ExecutableRemoveOperation
Start creating a remove operation for the given domainType.
Specified by:
remove
in interface
ExecutableRemoveOperation
Parameters:
domainType
- must not be null.
Returns:
new instance of
ExecutableRemoveOperation.ExecutableRemove
.
aggregateAndReturn
public
<T>
ExecutableAggregationOperation.ExecutableAggregation
<T>
aggregateAndReturn
(
Class
<T> domainType)
Description copied from interface:
ExecutableAggregationOperation
Start creating an aggregation operation that returns results mapped to the given domain type.
Use
TypedAggregation
to specify a potentially different
input type for he aggregation.
Specified by:
aggregateAndReturn
in interface
ExecutableAggregationOperation
Parameters:
domainType
- must not be null.
Returns:
new instance of
ExecutableAggregationOperation.ExecutableAggregation
.
mapReduce
Description copied from interface:
ExecutableMapReduceOperation
Start creating a mapReduce operation for the given domainType.
Specified by:
mapReduce
in interface
ExecutableMapReduceOperation
Parameters:
domainType
- must not be null.
Returns:
new instance of
ExecutableFindOperation.ExecutableFind
.
insert
Description copied from interface:
ExecutableInsertOperation
Start creating an insert operation for given domainType.
Specified by:
insert
in interface
ExecutableInsertOperation
Parameters:
domainType
- must not be null.
Returns:
new instance of
ExecutableInsertOperation.ExecutableInsert
.
getCollectionNames
Description copied from interface:
MongoOperations
A set of collection names.
Specified by:
getCollectionNames
in interface
MongoOperations
Returns:
list of collection names.
maybeCallBeforeSave
maybeCallAfterSave
maybeCallAfterConvert
doCreateCollection
protected
com.mongodb.client.MongoCollection<org.bson.Document>
doCreateCollection
(
String
collectionName,
org.bson.Document collectionOptions)
Create the specified collection using the provided options
Parameters:
collectionName
-
collectionOptions
-
Returns:
the collection that was created
doCreateCollection
protected
com.mongodb.client.MongoCollection<org.bson.Document>
doCreateCollection
(
String
collectionName,
com.mongodb.client.model.CreateCollectionOptions collectionOptions)
Create the specified collection using the provided options
Parameters:
collectionName
-
collectionOptions
-
Returns:
the collection that was created
Since:
3.3.3
protected
<T>
T
doFindOne
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
Class
<T> entityClass)
Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
The query document is specified as a standard
Document
and so is the fields specification.
Parameters:
collectionName
- name of the collection to retrieve the objects from.
query
- the query document that specifies the criteria used to find a record.
fields
- the document that specifies the fields to be returned.
entityClass
- the parameterized type of the returned list.
Returns:
the converted object or null if none exists.
protected
<T>
T
doFindOne
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
CursorPreparer
preparer,
Class
<T> entityClass)
Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
The query document is specified as a standard
Document
and so is the fields specification.
Parameters:
collectionName
- name of the collection to retrieve the objects from.
query
- the query document that specifies the criteria used to find a record.
fields
- the document that specifies the fields to be returned.
entityClass
- the parameterized type of the returned list.
preparer
- the preparer used to modify the cursor on execution.
Returns:
the converted object or null if none exists.
Since:
protected
<T>
List
<T>
doFind
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
Class
<T> entityClass)
Map the results of an ad-hoc query on the default MongoDB collection to a List using the template's converter. The
query document is specified as a standard Document and so is the fields specification.
Parameters:
collectionName
- name of the collection to retrieve the objects from
query
- the query document that specifies the criteria used to find a record
fields
- the document that specifies the fields to be returned
entityClass
- the parameterized type of the returned list.
Returns:
the List of converted objects.
protected
<T>
List
<T>
doFind
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
Class
<T> entityClass,
CursorPreparer
preparer)
Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. The object is
converted from the MongoDB native representation using an instance of . The query document is
specified as a standard Document and so is the fields specification.
Parameters:
collectionName
- name of the collection to retrieve the objects from.
query
- the query document that specifies the criteria used to find a record.
fields
- the document that specifies the fields to be returned.
entityClass
- the parameterized type of the returned list.
preparer
- allows for customization of the
FindIterable
used when iterating over the result set,
(apply limits, skips and so on).
Returns:
the
List
of converted objects.
T>
List
<T>
doFind
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
Class
<S> entityClass,
@Nullable
CursorPreparer
preparer,
org.springframework.data.mongodb.core.MongoTemplate.DocumentCallback<T> objectCallback)
convertToDocument
protected
org.bson.Document
convertToDocument
(
@Nullable
CollectionOptions
collectionOptions,
Class
<?> targetType)
Convert given
CollectionOptions
to a document and take the domain type information into account when
creating a mapped schema for validation.
Parameters:
collectionOptions
- can be null.
targetType
- must not be null. Use
Object
type instead.
Returns:
never null.
Since:
doFindAndRemove
protected
<T>
T
doFindAndRemove
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
org.bson.Document sort,
@Nullable
Collation
collation,
Class
<T> entityClass)
Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
The first document that matches the query is returned and also removed from the collection in the database.
The query document is specified as a standard Document and so is the fields specification.
Parameters:
collectionName
- name of the collection to retrieve the objects from
query
- the query document that specifies the criteria used to find a record
entityClass
- the parameterized type of the returned list.
Returns:
the List of converted objects.
doFindAndModify
protected
<T>
T
doFindAndModify
(
String
collectionName,
org.bson.Document query,
org.bson.Document fields,
org.bson.Document sort,
Class
<T> entityClass,
UpdateDefinition
update,
@Nullable
FindAndModifyOptions
options)
@Nullable
protected
<T>
T
doFindAndReplace
(
String
collectionName,
org.bson.Document mappedQuery,
org.bson.Document mappedFields,
org.bson.Document mappedSort,
@Nullable
com.mongodb.client.model.Collation collation,
Class
<?> entityType,
org.bson.Document replacement,
FindAndReplaceOptions
options,
Class
<T> resultType)
Customize this part for findAndReplace.
Parameters:
collectionName
- The name of the collection to perform the operation in.
mappedQuery
- the query to look up documents.
mappedFields
- the fields to project the result to.
mappedSort
- the sort to be applied when executing the query.
collation
- collation settings for the query. Can be null.
entityType
- the source domain type.
replacement
- the replacement
Document
.
options
- applicable options.
resultType
- the target domain type.
Returns:
null if object does not exist,
return new
is
false and
upsert
is false.
populateIdIfNecessary
Populates the id property of the saved object, if it's not set already.
Parameters:
savedObject
-
![]() |
纯真的保温杯 · 从JSON中获取最大值 9 月前 |
aggregate(TypedAggregation, Class)
.