@ -339,8 +339,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBCursor cursor = collection . find ( mappedQuery , mappedFields ) ;
DBCursor cursor = collection . find ( mappedQuery , mappedFields ) ;
QueryCursorPreparer cursorPreparer = new QueryCursorPreparer ( query , entityType ) ;
QueryCursorPreparer cursorPreparer = new QueryCursorPreparer ( query , entityType ) ;
ReadDbObjectCallback < T > readCallback = new ReadDbObjectCallback < T > ( mongoConverter , entityType , collection
ReadDbObjectCallback < T > readCallback = new ReadDbObjectCallback < T > ( mongoConverter , entityType ,
. getName ( ) ) ;
collection . getName ( ) ) ;
return new CloseableIterableCursorAdapter < T > ( cursorPreparer . prepare ( cursor ) , exceptionTranslator , readCallback ) ;
return new CloseableIterableCursorAdapter < T > ( cursorPreparer . prepare ( cursor ) , exceptionTranslator , readCallback ) ;
}
}
@ -373,8 +373,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* /
* /
@Deprecated
@Deprecated
public CommandResult executeCommand ( final DBObject command , final int options ) {
public CommandResult executeCommand ( final DBObject command , final int options ) {
return executeCommand ( command , ( options & Bytes . QUERYOPTION_SLAVEOK ) ! = 0 ? ReadPreference . secondaryPreferred ( )
return executeCommand ( command ,
: ReadPreference . primary ( ) ) ;
( options & Bytes . QUERYOPTION_SLAVEOK ) ! = 0 ? ReadPreference . secondaryPreferred ( ) : ReadPreference . primary ( ) ) ;
}
}
/ *
/ *
@ -422,7 +422,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* @param preparer allows for customization of the { @link DBCursor } used when iterating over the result set , ( apply
* @param preparer allows for customization of the { @link DBCursor } used when iterating over the result set , ( apply
* limits , skips and so on ) .
* limits , skips and so on ) .
* /
* /
protected void executeQuery ( Query query , String collectionName , DocumentCallbackHandler dch , CursorPreparer preparer ) {
protected void executeQuery ( Query query , String collectionName , DocumentCallbackHandler dch ,
CursorPreparer preparer ) {
Assert . notNull ( query ) ;
Assert . notNull ( query ) ;
@ -546,13 +547,27 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
}
public BulkOperations bulkOps ( BulkMode bulkMode , String collectionName ) {
public BulkOperations bulkOps ( BulkMode bulkMode , String collectionName ) {
return new DefaultBulkOperations ( this , bulkMode , collectionName , writeConcern ) ;
return bulkOps ( bulkMode , null , collectionName ) ;
}
}
public BulkOperations bulkOps ( BulkMode bulkMode , Class < ? > entityClass ) {
public BulkOperations bulkOps ( BulkMode bulkMode , Class < ? > entityClass ) {
return new DefaultBulkOperations ( this , bulkMode , determineCollectionName ( entityClass ) , writeConcern ) ;
return bulkOps ( bulkMode , entityClass , determineCollectionName ( entityClass ) ) ;
}
public BulkOperations bulkOps ( BulkMode mode , Class < ? > entityType , String collectionName ) {
Assert . notNull ( mode , "BulkMode must not be null!" ) ;
Assert . hasText ( collectionName , "Collection name must not be null or empty!" ) ;
DefaultBulkOperations operations = new DefaultBulkOperations ( this , mode , collectionName , entityType ) ;
operations . setExceptionTranslator ( exceptionTranslator ) ;
operations . setWriteConcernResolver ( writeConcernResolver ) ;
operations . setDefaultWriteConcern ( writeConcern ) ;
return operations ;
}
}
/ *
/ *
* ( non - Javadoc )
* ( non - Javadoc )
* @see org . springframework . data . mongodb . core . MongoOperations # scriptOps ( )
* @see org . springframework . data . mongodb . core . MongoOperations # scriptOps ( )
@ -646,8 +661,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
List < Object > results = ( List < Object > ) commandResult . get ( "results" ) ;
List < Object > results = ( List < Object > ) commandResult . get ( "results" ) ;
results = results = = null ? Collections . emptyList ( ) : results ;
results = results = = null ? Collections . emptyList ( ) : results ;
DbObjectCallback < GeoResult < T > > callback = new GeoNearResultDbObjectCallback < T > ( new ReadDbObjectCallback < T > (
DbObjectCallback < GeoResult < T > > callback = new GeoNearResultDbObjectCallback < T > (
mongoConverter , entityClass , collectionName ) , near . getMetric ( ) ) ;
new ReadDbObjectCallback < T > ( mongoConverter , entityClass , collectionName ) , near . getMetric ( ) ) ;
List < GeoResult < T > > result = new ArrayList < GeoResult < T > > ( results . size ( ) ) ;
List < GeoResult < T > > result = new ArrayList < GeoResult < T > > ( results . size ( ) ) ;
int index = 0 ;
int index = 0 ;
@ -724,8 +739,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
public long count ( Query query , Class < ? > entityClass , String collectionName ) {
public long count ( Query query , Class < ? > entityClass , String collectionName ) {
Assert . hasText ( collectionName ) ;
Assert . hasText ( collectionName ) ;
final DBObject dbObject = query = = null ? null : queryMapper . getMappedObject ( query . getQueryObject ( ) ,
final DBObject dbObject = query = = null ? null
entityClass = = null ? null : mappingContext . getPersistentEntity ( entityClass ) ) ;
: queryMapper . getMappedObject ( query . getQueryObject ( ) ,
entityClass = = null ? null : mappingContext . getPersistentEntity ( entityClass ) ) ;
return execute ( collectionName , new CollectionCallback < Long > ( ) {
return execute ( collectionName , new CollectionCallback < Long > ( ) {
public Long doInCollection ( DBCollection collection ) throws MongoException , DataAccessException {
public Long doInCollection ( DBCollection collection ) throws MongoException , DataAccessException {
@ -996,8 +1012,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . INSERT , collectionName ,
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . INSERT , collectionName ,
entityClass , dbDoc , null ) ;
entityClass , dbDoc , null ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . insert ( dbDoc ) : collection . insert ( dbDoc ,
WriteResult writeResult = writeConcernToUse = = null ? collection . insert ( dbDoc )
writeConcernToUse ) ;
: collection . insert ( dbDoc , writeConcernToUse ) ;
handleAnyWriteResultErrors ( writeResult , dbDoc , MongoActionOperation . INSERT ) ;
handleAnyWriteResultErrors ( writeResult , dbDoc , MongoActionOperation . INSERT ) ;
return dbDoc . get ( ID_FIELD ) ;
return dbDoc . get ( ID_FIELD ) ;
}
}
@ -1017,8 +1033,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . INSERT_LIST , collectionName , null ,
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . INSERT_LIST , collectionName , null ,
null , null ) ;
null , null ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . insert ( dbDocList ) : collection . insert (
WriteResult writeResult = writeConcernToUse = = null ? collection . insert ( dbDocList )
dbDocList . toArray ( ( DBObject [ ] ) new BasicDBObject [ dbDocList . size ( ) ] ) , writeConcernToUse ) ;
: collection . insert ( dbDocList . toArray ( ( DBObject [ ] ) new BasicDBObject [ dbDocList . size ( ) ] ) , writeConcernToUse ) ;
handleAnyWriteResultErrors ( writeResult , null , MongoActionOperation . INSERT_LIST ) ;
handleAnyWriteResultErrors ( writeResult , null , MongoActionOperation . INSERT_LIST ) ;
return null ;
return null ;
}
}
@ -1046,8 +1062,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . SAVE , collectionName , entityClass ,
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . SAVE , collectionName , entityClass ,
dbDoc , null ) ;
dbDoc , null ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . save ( dbDoc ) : collection . save ( dbDoc ,
WriteResult writeResult = writeConcernToUse = = null ? collection . save ( dbDoc )
writeConcernToUse ) ;
: collection . save ( dbDoc , writeConcernToUse ) ;
handleAnyWriteResultErrors ( writeResult , dbDoc , MongoActionOperation . SAVE ) ;
handleAnyWriteResultErrors ( writeResult , dbDoc , MongoActionOperation . SAVE ) ;
return dbDoc . get ( ID_FIELD ) ;
return dbDoc . get ( ID_FIELD ) ;
}
}
@ -1100,10 +1116,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
increaseVersionForUpdateIfNecessary ( entity , update ) ;
increaseVersionForUpdateIfNecessary ( entity , update ) ;
DBObject queryObj = query = = null ? new BasicDBObject ( ) : queryMapper . getMappedObject ( query . getQueryObject ( ) ,
DBObject queryObj = query = = null ? new BasicDBObject ( )
entity ) ;
: queryMapper . getMappedObject ( query . getQueryObject ( ) , entity ) ;
DBObject updateObj = update = = null ? new BasicDBObject ( ) : updateMapper . getMappedObject (
DBObject updateObj = update = = null ? new BasicDBObject ( )
update . getUpdateObject ( ) , entity ) ;
: updateMapper . getMappedObject ( update . getUpdateObject ( ) , entity ) ;
if ( LOGGER . isDebugEnabled ( ) ) {
if ( LOGGER . isDebugEnabled ( ) ) {
LOGGER . debug ( String . format ( "Calling update using query: %s and update: %s in collection: %s" ,
LOGGER . debug ( String . format ( "Calling update using query: %s and update: %s in collection: %s" ,
@ -1244,9 +1260,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
Object idValue = persistentEntity . getPropertyAccessor ( entity ) . getProperty ( idProperty ) ;
Object idValue = persistentEntity . getPropertyAccessor ( entity ) . getProperty ( idProperty ) ;
if ( idValue = = null & & ! MongoSimpleTypes . AUTOGENERATED_ID_TYPES . contains ( idProperty . getType ( ) ) ) {
if ( idValue = = null & & ! MongoSimpleTypes . AUTOGENERATED_ID_TYPES . contains ( idProperty . getType ( ) ) ) {
throw new InvalidDataAccessApiUsageException ( String . format (
throw new InvalidDataAccessApiUsageException (
"Cannot autogenerate id of type %s for entity of type %s!" , idProperty . getType ( ) . getName ( ) , entity . getClass ( )
String . format ( "Cannot autogenerate id of type %s for entity of type %s!" , idProperty . getType ( ) . getName ( ) ,
. getName ( ) ) ) ;
entity . getClass ( ) . getName ( ) ) ) ;
}
}
}
}
@ -1285,12 +1301,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
if ( LOGGER . isDebugEnabled ( ) ) {
if ( LOGGER . isDebugEnabled ( ) ) {
LOGGER . debug ( "Remove using query: {} in collection: {}." , new Object [ ] { serializeToJsonSafely ( dboq ) ,
LOGGER . debug ( "Remove using query: {} in collection: {}." ,
collection . getName ( ) } ) ;
new Object [ ] { serializeToJsonSafely ( dboq ) , collection . getName ( ) } ) ;
}
}
WriteResult wr = writeConcernToUse = = null ? collection . remove ( dboq ) : collection . remove ( dboq ,
WriteResult wr = writeConcernToUse = = null ? collection . remove ( dboq )
writeConcernToUse ) ;
: collection . remove ( dboq , writeConcernToUse ) ;
handleAnyWriteResultErrors ( wr , dboq , MongoActionOperation . REMOVE ) ;
handleAnyWriteResultErrors ( wr , dboq , MongoActionOperation . REMOVE ) ;
@ -1306,8 +1322,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
}
public < T > List < T > findAll ( Class < T > entityClass , String collectionName ) {
public < T > List < T > findAll ( Class < T > entityClass , String collectionName ) {
return executeFindMultiInternal ( new FindCallback ( null ) , null , new ReadDbObjectCallback < T > ( mongoConverter ,
return executeFindMultiInternal ( new FindCallback ( null ) , null ,
entityClass , collectionName ) , collectionName ) ;
new ReadDbObjectCallback < T > ( mongoConverter , entityClass , collectionName ) , collectionName ) ;
}
}
public < T > MapReduceResults < T > mapReduce ( String inputCollectionName , String mapFunction , String reduceFunction ,
public < T > MapReduceResults < T > mapReduce ( String inputCollectionName , String mapFunction , String reduceFunction ,
@ -1323,8 +1339,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
public < T > MapReduceResults < T > mapReduce ( Query query , String inputCollectionName , String mapFunction ,
public < T > MapReduceResults < T > mapReduce ( Query query , String inputCollectionName , String mapFunction ,
String reduceFunction , Class < T > entityClass ) {
String reduceFunction , Class < T > entityClass ) {
return mapReduce ( query , inputCollectionName , mapFunction , reduceFunction ,
return mapReduce ( query , inputCollectionName , mapFunction , reduceFunction , new MapReduceOptions ( ) . outputTypeInline ( ) ,
new MapReduceOptions ( ) . outputTypeInline ( ) , entityClass ) ;
entityClass ) ;
}
}
public < T > MapReduceResults < T > mapReduce ( Query query , String inputCollectionName , String mapFunction ,
public < T > MapReduceResults < T > mapReduce ( Query query , String inputCollectionName , String mapFunction ,
@ -1335,8 +1351,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBCollection inputCollection = getCollection ( inputCollectionName ) ;
DBCollection inputCollection = getCollection ( inputCollectionName ) ;
MapReduceCommand command = new MapReduceCommand ( inputCollection , mapFunc , reduceFunc ,
MapReduceCommand command = new MapReduceCommand ( inputCollection , mapFunc , reduceFunc ,
mapReduceOptions . getOutputCollection ( ) , mapReduceOptions . getOutputType ( ) , query = = null
mapReduceOptions . getOutputCollection ( ) , mapReduceOptions . getOutputType ( ) ,
| | query . getQueryObject ( ) = = null ? null : queryMapper . getMappedObject ( query . getQueryObject ( ) , null ) ) ;
query = = null | | query . getQueryObject ( ) = = null ? null
: queryMapper . getMappedObject ( query . getQueryObject ( ) , null ) ) ;
copyMapReduceOptionsToCommand ( query , mapReduceOptions , command ) ;
copyMapReduceOptionsToCommand ( query , mapReduceOptions , command ) ;
@ -1669,8 +1686,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
serializeToJsonSafely ( query ) , mappedFields , entityClass , collectionName ) ) ;
serializeToJsonSafely ( query ) , mappedFields , entityClass , collectionName ) ) ;
}
}
return executeFindOneInternal ( new FindOneCallback ( mappedQuery , mappedFields ) , new ReadDbObjectCallback < T > (
return executeFindOneInternal ( new FindOneCallback ( mappedQuery , mappedFields ) ,
this . mongoConverter , entityClass , collectionName ) , collectionName ) ;
new ReadDbObjectCallback < T > ( this . mongoConverter , entityClass , collectionName ) , collectionName ) ;
}
}
/ * *
/ * *
@ -1684,8 +1701,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* @return the List of converted objects .
* @return the List of converted objects .
* /
* /
protected < T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < T > entityClass ) {
protected < T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < T > entityClass ) {
return doFind ( collectionName , query , fields , entityClass , null , new ReadDbObjectCallback < T > ( this . mongoConverter ,
return doFind ( collectionName , query , fields , entityClass , null ,
entityClass , collectionName ) ) ;
new ReadDbObjectCallback < T > ( this . mongoConverter , entityClass , collectionName ) ) ;
}
}
/ * *
/ * *
@ -1703,8 +1720,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* /
* /
protected < T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < T > entityClass ,
protected < T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < T > entityClass ,
CursorPreparer preparer ) {
CursorPreparer preparer ) {
return doFind ( collectionName , query , fields , entityClass , preparer , new ReadDbObjectCallback < T > ( mongoConverter ,
return doFind ( collectionName , query , fields , entityClass , preparer ,
entityClass , collectionName ) ) ;
new ReadDbObjectCallback < T > ( mongoConverter , entityClass , collectionName ) ) ;
}
}
protected < S , T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < S > entityClass ,
protected < S , T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < S > entityClass ,
@ -1780,9 +1797,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBObject mappedUpdate = updateMapper . getMappedObject ( update . getUpdateObject ( ) , entity ) ;
DBObject mappedUpdate = updateMapper . getMappedObject ( update . getUpdateObject ( ) , entity ) ;
if ( LOGGER . isDebugEnabled ( ) ) {
if ( LOGGER . isDebugEnabled ( ) ) {
LOGGER . debug ( String . format ( "findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s "
LOGGER
+ "in collection: %s" , serializeToJsonSafely ( mappedQuery ) , fields , sort , entityClass ,
. debug (
serializeToJsonSafely ( mappedUpdate ) , collectionName ) ) ;
String . format (
"findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s "
+ "in collection: %s" ,
serializeToJsonSafely ( mappedQuery ) , fields , sort , entityClass , serializeToJsonSafely ( mappedUpdate ) ,
collectionName ) ) ;
}
}
return executeFindOneInternal ( new FindAndModifyCallback ( mappedQuery , fields , sort , mappedUpdate , options ) ,
return executeFindOneInternal ( new FindAndModifyCallback ( mappedQuery , fields , sort , mappedUpdate , options ) ,
@ -1852,8 +1873,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DbObjectCallback < T > objectCallback , String collectionName ) {
DbObjectCallback < T > objectCallback , String collectionName ) {
try {
try {
T result = objectCallback . doWith ( collectionCallback . doInCollection ( getAndPrepareCollection ( getDb ( ) ,
T result = objectCallback
collectionName ) ) ) ;
. doWith ( collectionCallback . doInCollection ( getAndPrepareCollection ( getDb ( ) , collectionName ) ) ) ;
return result ;
return result ;
} catch ( RuntimeException e ) {
} catch ( RuntimeException e ) {
throw potentiallyConvertRuntimeException ( e , exceptionTranslator ) ;
throw potentiallyConvertRuntimeException ( e , exceptionTranslator ) ;
@ -1878,8 +1899,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* @param collectionName the collection to be queried
* @param collectionName the collection to be queried
* @return
* @return
* /
* /
private < T > List < T > executeFindMultiInternal ( CollectionCallback < DBCursor > collectionCallback ,
private < T > List < T > executeFindMultiInternal ( CollectionCallback < DBCursor > collectionCallback , CursorPreparer preparer ,
CursorPreparer preparer , DbObjectCallback < T > objectCallback , String collectionName ) {
DbObjectCallback < T > objectCallback , String collectionName ) {
try {
try {
@ -1969,8 +1990,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoPersistentEntity < ? > entity = mappingContext . getPersistentEntity ( entityClass ) ;
MongoPersistentEntity < ? > entity = mappingContext . getPersistentEntity ( entityClass ) ;
if ( entity = = null ) {
if ( entity = = null ) {
throw new InvalidDataAccessApiUsageException ( "No Persistent Entity information found for the class "
throw new InvalidDataAccessApiUsageException (
+ entityClass . getName ( ) ) ;
"No Persistent Entity information found for the class " + entityClass . getName ( ) ) ;
}
}
return entity . getCollection ( ) ;
return entity . getCollection ( ) ;
}
}
@ -2034,8 +2055,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
String error = result . getErrorMessage ( ) ;
String error = result . getErrorMessage ( ) ;
error = error = = null ? "NO MESSAGE" : error ;
error = error = = null ? "NO MESSAGE" : error ;
throw new InvalidDataAccessApiUsageException ( "Command execution failed: Error [" + error + "], Command = "
throw new InvalidDataAccessApiUsageException (
+ source , ex ) ;
"Command execution failed: Error [" + error + "], Command = " + source , ex ) ;
}
}
}
}
@ -2231,7 +2252,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
class UnwrapAndReadDbObjectCallback < T > extends ReadDbObjectCallback < T > {
class UnwrapAndReadDbObjectCallback < T > extends ReadDbObjectCallback < T > {
public UnwrapAndReadDbObjectCallback ( EntityReader < ? super T , DBObject > reader , Class < T > type , String collectionName ) {
public UnwrapAndReadDbObjectCallback ( EntityReader < ? super T , DBObject > reader , Class < T > type ,
String collectionName ) {
super ( reader , type , collectionName ) ;
super ( reader , type , collectionName ) ;
}
}
@ -2258,15 +2280,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
}
}
}
private enum DefaultWriteConcernResolver implements WriteConcernResolver {
INSTANCE ;
public WriteConcern resolve ( MongoAction action ) {
return action . getDefaultWriteConcern ( ) ;
}
}
class QueryCursorPreparer implements CursorPreparer {
class QueryCursorPreparer implements CursorPreparer {
private final Query query ;
private final Query query ;