@@ -224,7 +224,7 @@ public MongoTemplate(MongoDatabaseFactory mongoDbFactory, @Nullable MongoConvert
224
224
this .queryMapper = new QueryMapper (this .mongoConverter );
225
225
this .updateMapper = new UpdateMapper (this .mongoConverter );
226
226
this .schemaMapper = new MongoJsonSchemaMapper (this .mongoConverter );
227
- this .operations = new EntityOperations (this .mongoConverter );
227
+ this .operations = new EntityOperations (this .mongoConverter , this . queryMapper );
228
228
this .propertyOperations = new PropertyOperations (this .mongoConverter .getMappingContext ());
229
229
this .queryOperations = new QueryOperations (queryMapper , updateMapper , operations , propertyOperations ,
230
230
mongoDbFactory );
@@ -595,13 +595,8 @@ public <T> MongoCollection<Document> createCollection(Class<T> entityClass,
595
595
596
596
Assert .notNull (entityClass , "EntityClass must not be null!" );
597
597
598
- CollectionOptions options = collectionOptions != null ? collectionOptions : CollectionOptions .empty ();
599
- options = Optionals
600
- .firstNonEmpty (() -> Optional .ofNullable (collectionOptions ).flatMap (CollectionOptions ::getCollation ),
601
- () -> operations .forType (entityClass ).getCollation ()) //
602
- .map (options ::collation ).orElse (options );
603
-
604
- return doCreateCollection (getCollectionName (entityClass ), convertToDocument (options , entityClass ));
598
+ return doCreateCollection (getCollectionName (entityClass ),
599
+ operations .convertToCreateCollectionOptions (collectionOptions , entityClass ));
605
600
}
606
601
607
602
@ Override
@@ -617,7 +612,8 @@ public MongoCollection<Document> createCollection(String collectionName,
617
612
@ Nullable CollectionOptions collectionOptions ) {
618
613
619
614
Assert .notNull (collectionName , "CollectionName must not be null!" );
620
- return doCreateCollection (collectionName , convertToDocument (collectionOptions , Object .class ));
615
+ return doCreateCollection (collectionName ,
616
+ operations .convertToCreateCollectionOptions (collectionOptions , Object .class ));
621
617
}
622
618
623
619
@ Override
@@ -2225,64 +2221,82 @@ protected <T> T maybeCallAfterConvert(T object, Document document, String collec
2225
2221
*/
2226
2222
@ SuppressWarnings ("ConstantConditions" )
2227
2223
protected MongoCollection <Document > doCreateCollection (String collectionName , Document collectionOptions ) {
2224
+ return doCreateCollection (collectionName , getCreateCollectionOptions (collectionOptions ));
2225
+ }
2226
+
2227
+ /**
2228
+ * Create the specified collection using the provided options
2229
+ *
2230
+ * @param collectionName
2231
+ * @param collectionOptions
2232
+ * @return the collection that was created
2233
+ * @since 3.3.3
2234
+ */
2235
+ @ SuppressWarnings ("ConstantConditions" )
2236
+ protected MongoCollection <Document > doCreateCollection (String collectionName ,
2237
+ CreateCollectionOptions collectionOptions ) {
2238
+
2228
2239
return execute (db -> {
2229
2240
2230
- CreateCollectionOptions co = new CreateCollectionOptions ( );
2241
+ db . createCollection ( collectionName , collectionOptions );
2231
2242
2232
- if (collectionOptions .containsKey ("capped" )) {
2233
- co .capped ((Boolean ) collectionOptions .get ("capped" ));
2234
- }
2235
- if (collectionOptions .containsKey ("size" )) {
2236
- co .sizeInBytes (((Number ) collectionOptions .get ("size" )).longValue ());
2237
- }
2238
- if (collectionOptions .containsKey ("max" )) {
2239
- co .maxDocuments (((Number ) collectionOptions .get ("max" )).longValue ());
2240
- }
2243
+ MongoCollection <Document > coll = db .getCollection (collectionName , Document .class );
2241
2244
2242
- if (collectionOptions .containsKey ("collation" )) {
2243
- co .collation (IndexConverters .fromDocument (collectionOptions .get ("collation" , Document .class )));
2245
+ // TODO: Emit a collection created event
2246
+ if (LOGGER .isDebugEnabled ()) {
2247
+ LOGGER .debug (String .format ("Created collection [%s]" ,
2248
+ coll .getNamespace () != null ? coll .getNamespace ().getCollectionName () : collectionName ));
2244
2249
}
2250
+ return coll ;
2251
+ });
2252
+ }
2245
2253
2246
- if ( collectionOptions . containsKey ( "validator" ) ) {
2254
+ private CreateCollectionOptions getCreateCollectionOptions ( Document document ) {
2247
2255
2248
- com . mongodb . client . model . ValidationOptions options = new com . mongodb . client . model . ValidationOptions ();
2256
+ CreateCollectionOptions options = new CreateCollectionOptions ();
2249
2257
2250
- if (collectionOptions .containsKey ("validationLevel" )) {
2251
- options .validationLevel (ValidationLevel .fromString (collectionOptions .getString ("validationLevel" )));
2252
- }
2253
- if (collectionOptions .containsKey ("validationAction" )) {
2254
- options .validationAction (ValidationAction .fromString (collectionOptions .getString ("validationAction" )));
2255
- }
2258
+ if (document .containsKey ("capped" )) {
2259
+ options .capped ((Boolean ) document .get ("capped" ));
2260
+ }
2261
+ if (document .containsKey ("size" )) {
2262
+ options .sizeInBytes (((Number ) document .get ("size" )).longValue ());
2263
+ }
2264
+ if (document .containsKey ("max" )) {
2265
+ options .maxDocuments (((Number ) document .get ("max" )).longValue ());
2266
+ }
2256
2267
2257
- options . validator ( collectionOptions . get ( "validator" , Document . class ));
2258
- co . validationOptions ( options );
2259
- }
2268
+ if ( document . containsKey ( "collation" )) {
2269
+ options . collation ( IndexConverters . fromDocument ( document . get ( "collation" , Document . class )) );
2270
+ }
2260
2271
2261
- if (collectionOptions .containsKey ("timeseries " )) {
2272
+ if (document .containsKey ("validator " )) {
2262
2273
2263
- Document timeSeries = collectionOptions .get ("timeseries" , Document .class );
2264
- com .mongodb .client .model .TimeSeriesOptions options = new com .mongodb .client .model .TimeSeriesOptions (
2265
- timeSeries .getString ("timeField" ));
2266
- if (timeSeries .containsKey ("metaField" )) {
2267
- options .metaField (timeSeries .getString ("metaField" ));
2268
- }
2269
- if (timeSeries .containsKey ("granularity" )) {
2270
- options .granularity (TimeSeriesGranularity .valueOf (timeSeries .getString ("granularity" ).toUpperCase ()));
2271
- }
2272
- co .timeSeriesOptions (options );
2274
+ ValidationOptions validation = new ValidationOptions ();
2275
+
2276
+ if (document .containsKey ("validationLevel" )) {
2277
+ validation .validationLevel (ValidationLevel .fromString (document .getString ("validationLevel" )));
2278
+ }
2279
+ if (document .containsKey ("validationAction" )) {
2280
+ validation .validationAction (ValidationAction .fromString (document .getString ("validationAction" )));
2273
2281
}
2274
2282
2275
- db .createCollection (collectionName , co );
2283
+ validation .validator (document .get ("validator" , Document .class ));
2284
+ options .validationOptions (validation );
2285
+ }
2276
2286
2277
- MongoCollection < Document > coll = db . getCollection ( collectionName , Document . class );
2287
+ if ( document . containsKey ( "timeseries" )) {
2278
2288
2279
- // TODO: Emit a collection created event
2280
- if ( LOGGER . isDebugEnabled ()) {
2281
- LOGGER . debug ( String . format ( "Created collection [%s]" ,
2282
- coll . getNamespace () != null ? coll . getNamespace (). getCollectionName () : collectionName ));
2289
+ Document timeSeries = document . get ( "timeseries" , Document . class );
2290
+ TimeSeriesOptions timeseries = new TimeSeriesOptions ( timeSeries . getString ( "timeField" ));
2291
+ if ( timeSeries . containsKey ( "metaField" )) {
2292
+ timeseries . metaField ( timeSeries . getString ( "metaField" ));
2283
2293
}
2284
- return coll ;
2285
- });
2294
+ if (timeSeries .containsKey ("granularity" )) {
2295
+ timeseries .granularity (TimeSeriesGranularity .valueOf (timeSeries .getString ("granularity" ).toUpperCase ()));
2296
+ }
2297
+ options .timeSeriesOptions (timeseries );
2298
+ }
2299
+ return options ;
2286
2300
}
2287
2301
2288
2302
/**
0 commit comments