99
99
import org .springframework .data .mongodb .core .query .Meta ;
100
100
import org .springframework .data .mongodb .core .query .NearQuery ;
101
101
import org .springframework .data .mongodb .core .query .Query ;
102
+ import org .springframework .data .mongodb .core .query .SerializationUtils ;
102
103
import org .springframework .data .mongodb .core .query .UpdateDefinition ;
103
104
import org .springframework .data .mongodb .core .query .UpdateDefinition .ArrayFilter ;
104
105
import org .springframework .data .mongodb .core .timeseries .Granularity ;
179
180
* @author Bartłomiej Mazur
180
181
* @author Michael Krog
181
182
* @author Jakub Zurawa
183
+ * @author Marcin Grzejszczak
182
184
*/
183
185
public class MongoTemplate
184
186
implements MongoOperations , ApplicationContextAware , IndexOperationsProvider , ReadPreferenceAware {
@@ -534,7 +536,7 @@ public Document executeCommand(Document command) {
534
536
535
537
if (LOGGER .isDebugEnabled ()) {
536
538
LOGGER .debug (String .format (
537
- "Executing command: [%s]" , command . toJson ( )));
539
+ "Executing command: [%s]" , serializeToJsonSafely ( command )));
538
540
}
539
541
540
542
return execute (db -> db .runCommand (command , Document .class ));
@@ -548,7 +550,7 @@ public Document executeCommand(Document command, @Nullable ReadPreference readPr
548
550
549
551
if (LOGGER .isDebugEnabled ()) {
550
552
LOGGER .debug (String .format (
551
- "Executing command: [%s]%s" , command . toJson ( ), readPreference != null ? (" with read preference: [" + readPreference + "]" ) : "" ));
553
+ "Executing command: [%s]%s" , serializeToJsonSafely ( command ), readPreference != null ? (" with read preference: [" + readPreference + "]" ) : "" ));
552
554
}
553
555
554
556
return execute (db -> readPreference != null //
@@ -3069,8 +3071,8 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
3069
3071
3070
3072
if (LOGGER .isDebugEnabled ()) {
3071
3073
LOGGER .debug (String .format (
3072
- "Applied callback [%s] query [%s], fields [%s]. Will return first result. " , getClass ().getSimpleName (), query . toJson ( ), fields .map (
3073
- Document :: toJson ).orElse ("" )));
3074
+ "Applied callback [%s] query [%s], fields [%s]" , getClass ().getSimpleName (), serializeToJsonSafely ( query ), fields .map (
3075
+ SerializationUtils :: serializeToJsonSafely ).orElse ("" )));
3074
3076
}
3075
3077
3076
3078
return iterable .first ();
@@ -3117,7 +3119,7 @@ public FindIterable<Document> doInCollection(MongoCollection<Document> collectio
3117
3119
3118
3120
if (LOGGER .isDebugEnabled ()) {
3119
3121
LOGGER .debug (String .format (
3120
- "Applied callback [%s] query [%s], collation [%s]. Will return first result. " , getClass ().getSimpleName (), query . toJson ( ), collation ));
3122
+ "Applied callback [%s] query [%s], collation [%s]" , getClass ().getSimpleName (), serializeToJsonSafely ( query ), serializeToJsonSafely ( collation ) ));
3121
3123
}
3122
3124
3123
3125
return findIterable ;
@@ -3147,9 +3149,15 @@ private class ExistsCallback implements CollectionCallback<Boolean> {
3147
3149
3148
3150
@ Override
3149
3151
public Boolean doInCollection (MongoCollection <Document > collection ) throws MongoException , DataAccessException {
3150
-
3151
- return doCount (collectionPreparer , collection .getNamespace ().getCollectionName (), mappedQuery ,
3152
+ boolean positiveCount = doCount (collectionPreparer , collection .getNamespace ().getCollectionName (), mappedQuery ,
3152
3153
new CountOptions ().limit (1 ).collation (collation )) > 0 ;
3154
+
3155
+ if (LOGGER .isDebugEnabled ()) {
3156
+ LOGGER .debug (String .format (
3157
+ "Applied callback [%s] query [%s], collation [%s]" , getClass ().getSimpleName (), serializeToJsonSafely (mappedQuery ), serializeToJsonSafely (collation )));
3158
+ }
3159
+
3160
+ return positiveCount ;
3153
3161
}
3154
3162
}
3155
3163
@@ -3183,7 +3191,14 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
3183
3191
FindOneAndDeleteOptions opts = new FindOneAndDeleteOptions ().sort (sort ).projection (fields );
3184
3192
collation .map (Collation ::toMongoCollation ).ifPresent (opts ::collation );
3185
3193
3186
- return collectionPreparer .prepare (collection ).findOneAndDelete (query , opts );
3194
+ Document oneAndDelete = collectionPreparer .prepare (collection ).findOneAndDelete (query , opts );
3195
+
3196
+ if (LOGGER .isDebugEnabled ()) {
3197
+ LOGGER .debug (String .format (
3198
+ "Applied callback [%s] query [%s], fields [%s], sort [%s], collation [%s]" , getClass ().getSimpleName (), serializeToJsonSafely (query ), serializeToJsonSafely (fields ), serializeToJsonSafely (sort ), serializeToJsonSafely (collation )));
3199
+ }
3200
+
3201
+ return oneAndDelete ;
3187
3202
}
3188
3203
}
3189
3204
@@ -3228,13 +3243,21 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
3228
3243
opts .arrayFilters (arrayFilters );
3229
3244
}
3230
3245
3246
+ Document result ;
3231
3247
if (update instanceof Document document ) {
3232
- return collectionPreparer .prepare (collection ).findOneAndUpdate (query , document , opts );
3248
+ result = collectionPreparer .prepare (collection ).findOneAndUpdate (query , document , opts );
3233
3249
} else if (update instanceof List ) {
3234
- return collectionPreparer .prepare (collection ).findOneAndUpdate (query , (List <Document >) update , opts );
3250
+ result = collectionPreparer .prepare (collection ).findOneAndUpdate (query , (List <Document >) update , opts );
3251
+ } else {
3252
+ throw new IllegalArgumentException (String .format ("Using %s is not supported in findOneAndUpdate" , update ));
3235
3253
}
3236
3254
3237
- throw new IllegalArgumentException (String .format ("Using %s is not supported in findOneAndUpdate" , update ));
3255
+ if (LOGGER .isDebugEnabled ()) {
3256
+ LOGGER .debug (String .format (
3257
+ "Applied callback [%s] query [%s], fields [%s], sort [%s], update [%s], arrayFilters [%s], options [%s]" , getClass ().getSimpleName (), serializeToJsonSafely (query ), serializeToJsonSafely (fields ), serializeToJsonSafely (sort ), serializeToJsonSafely (update ), serializeToJsonSafely (arrayFilters ), serializeToJsonSafely (options )));
3258
+ }
3259
+
3260
+ return result ;
3238
3261
}
3239
3262
}
3240
3263
@@ -3283,7 +3306,14 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
3283
3306
opts .returnDocument (ReturnDocument .AFTER );
3284
3307
}
3285
3308
3286
- return collectionPreparer .prepare (collection ).findOneAndReplace (query , update , opts );
3309
+ Document document = collectionPreparer .prepare (collection ).findOneAndReplace (query , update , opts );
3310
+
3311
+ if (LOGGER .isDebugEnabled ()) {
3312
+ LOGGER .debug (String .format (
3313
+ "Applied callback [%s] query [%s], fields [%s], sort [%s], update [%s], collation [%s], options [%s]" , getClass ().getSimpleName (), serializeToJsonSafely (query ), serializeToJsonSafely (fields ), serializeToJsonSafely (sort ), serializeToJsonSafely (update ), serializeToJsonSafely (collation ), serializeToJsonSafely (options )));
3314
+ }
3315
+
3316
+ return document ;
3287
3317
}
3288
3318
}
3289
3319
@@ -3333,6 +3363,11 @@ public T doWith(Document document) {
3333
3363
maybeEmitEvent (new AfterConvertEvent <>(document , entity , collectionName ));
3334
3364
entity = maybeCallAfterConvert (entity , document , collectionName );
3335
3365
3366
+ if (LOGGER .isDebugEnabled ()) {
3367
+ LOGGER .debug (String .format (
3368
+ "Applied callback [%s] reader [%s], type [%s], collectionName [%s]" , getClass ().getSimpleName (), serializeToJsonSafely (reader ), serializeToJsonSafely (type ), serializeToJsonSafely (collectionName )));
3369
+ }
3370
+
3336
3371
return entity ;
3337
3372
}
3338
3373
}
@@ -3375,7 +3410,14 @@ public T doWith(Document document) {
3375
3410
}
3376
3411
3377
3412
maybeEmitEvent (new AfterConvertEvent <>(document , entity , collectionName ));
3378
- return (T ) maybeCallAfterConvert (entity , document , collectionName );
3413
+ T maybeConverted = (T ) maybeCallAfterConvert (entity , document , collectionName );
3414
+
3415
+ if (LOGGER .isDebugEnabled ()) {
3416
+ LOGGER .debug (String .format (
3417
+ "Applied callback [%s] converter [%s], projection [%s], collectionName [%s]" , getClass ().getSimpleName (), serializeToJsonSafely (mongoConverter ), serializeToJsonSafely (projection ), serializeToJsonSafely (collectionName )));
3418
+ }
3419
+
3420
+ return maybeConverted ;
3379
3421
}
3380
3422
}
3381
3423
@@ -3474,6 +3516,11 @@ public FindIterable<Document> prepare(FindIterable<Document> iterable) {
3474
3516
throw potentiallyConvertRuntimeException (e , exceptionTranslator );
3475
3517
}
3476
3518
3519
+ if (LOGGER .isDebugEnabled ()) {
3520
+ LOGGER .debug (String .format (
3521
+ "Prepared cursor with [%s] query [%s], sortObject [%s], limit [%s], skip [%s], type [%s]" , getClass ().getSimpleName (), serializeToJsonSafely (query ), serializeToJsonSafely (sortObject ), serializeToJsonSafely (limit ), serializeToJsonSafely (skip ), serializeToJsonSafely (type )));
3522
+ }
3523
+
3477
3524
return cursorToUse ;
3478
3525
}
3479
3526
@@ -3519,7 +3566,14 @@ public GeoResult<T> doWith(Document object) {
3519
3566
3520
3567
T doWith = delegate .doWith (object );
3521
3568
3522
- return new GeoResult <>(doWith , new Distance (distance , metric ));
3569
+ GeoResult <T > tGeoResult = new GeoResult <>(doWith , new Distance (distance , metric ));
3570
+
3571
+ if (LOGGER .isDebugEnabled ()) {
3572
+ LOGGER .debug (String .format (
3573
+ "Applied callback [%s] distanceField [%s], delegate [%s], metric [%s]" , getClass ().getSimpleName (), serializeToJsonSafely (distanceField ), serializeToJsonSafely (delegate ), serializeToJsonSafely (metric )));
3574
+ }
3575
+
3576
+ return tGeoResult ;
3523
3577
}
3524
3578
}
3525
3579
@@ -3684,7 +3738,14 @@ private static class ReplaceCallback implements CollectionCallback<UpdateResult>
3684
3738
@ Override
3685
3739
public UpdateResult doInCollection (MongoCollection <Document > collection )
3686
3740
throws MongoException , DataAccessException {
3687
- return collectionPreparer .prepare (collection ).replaceOne (query , update , options );
3741
+ UpdateResult updateResult = collectionPreparer .prepare (collection ).replaceOne (query , update , options );
3742
+
3743
+ if (LOGGER .isDebugEnabled ()) {
3744
+ LOGGER .debug (String .format (
3745
+ "Applied callback [%s] collectionPreparer [%s], query [%s], update [%s], options [%s]" , getClass ().getSimpleName (), collectionPreparer .getClass ().getSimpleName (), serializeToJsonSafely (query ), serializeToJsonSafely (update ), serializeToJsonSafely (options )));
3746
+ }
3747
+
3748
+ return updateResult ;
3688
3749
}
3689
3750
}
3690
3751
}
0 commit comments