@@ -648,13 +648,8 @@ public <T> MongoCollection<Document> createCollection(Class<T> entityClass,
648648
649649 Assert .notNull (entityClass , "EntityClass must not be null!" );
650650
651- CollectionOptions options = collectionOptions != null ? collectionOptions : CollectionOptions .empty ();
652- options = Optionals
653- .firstNonEmpty (() -> Optional .ofNullable (collectionOptions ).flatMap (CollectionOptions ::getCollation ),
654- () -> operations .forType (entityClass ).getCollation ()) //
655- .map (options ::collation ).orElse (options );
656-
657- return doCreateCollection (getCollectionName (entityClass ), convertToDocument (options , entityClass ));
651+ return doCreateCollection (getCollectionName (entityClass ),
652+ operations .convertToCreateCollectionOptions (collectionOptions , entityClass ));
658653 }
659654
660655 /*
@@ -676,7 +671,8 @@ public MongoCollection<Document> createCollection(String collectionName,
676671 @ Nullable CollectionOptions collectionOptions ) {
677672
678673 Assert .notNull (collectionName , "CollectionName must not be null!" );
679- return doCreateCollection (collectionName , convertToDocument (collectionOptions , Object .class ));
674+ return doCreateCollection (collectionName ,
675+ operations .convertToCreateCollectionOptions (collectionOptions , Object .class ));
680676 }
681677
682678 /*
@@ -2475,66 +2471,81 @@ protected <T> T maybeCallAfterConvert(T object, Document document, String collec
24752471 * @param collectionOptions
24762472 * @return the collection that was created
24772473 */
2478- @ SuppressWarnings ("ConstantConditions" )
24792474 protected MongoCollection <Document > doCreateCollection (String collectionName , Document collectionOptions ) {
2475+ return doCreateCollection (collectionName , getCreateCollectionOptions (collectionOptions ));
2476+ }
2477+
2478+ /**
2479+ * Create the specified collection using the provided options
2480+ *
2481+ * @param collectionName
2482+ * @param collectionOptions
2483+ * @return the collection that was created
2484+ * @since 3.3.3
2485+ */
2486+ @ SuppressWarnings ("ConstantConditions" )
2487+ protected MongoCollection <Document > doCreateCollection (String collectionName ,
2488+ CreateCollectionOptions collectionOptions ) {
24802489 return execute (db -> {
2490+ db .createCollection (collectionName , collectionOptions );
24812491
2482- CreateCollectionOptions co = new CreateCollectionOptions ( );
2492+ MongoCollection < Document > coll = db . getCollection ( collectionName , Document . class );
24832493
2484- if (collectionOptions .containsKey ("capped" )) {
2485- co .capped ((Boolean ) collectionOptions .get ("capped" ));
2486- }
2487- if (collectionOptions .containsKey ("size" )) {
2488- co .sizeInBytes (((Number ) collectionOptions .get ("size" )).longValue ());
2489- }
2490- if (collectionOptions .containsKey ("max" )) {
2491- co .maxDocuments (((Number ) collectionOptions .get ("max" )).longValue ());
2494+ // TODO: Emit a collection created event
2495+ if (LOGGER .isDebugEnabled ()) {
2496+ LOGGER .debug (String .format ("Created collection [%s]" ,
2497+ coll .getNamespace () != null ? coll .getNamespace ().getCollectionName () : collectionName ));
24922498 }
2499+ return coll ;
2500+ });
2501+ }
24932502
2494- if (collectionOptions .containsKey ("collation" )) {
2495- co .collation (IndexConverters .fromDocument (collectionOptions .get ("collation" , Document .class )));
2496- }
2503+ private CreateCollectionOptions getCreateCollectionOptions (Document collectionOptions ) {
24972504
2498- if ( collectionOptions . containsKey ( "validator" )) {
2505+ CreateCollectionOptions co = new CreateCollectionOptions ();
24992506
2500- com .mongodb .client .model .ValidationOptions options = new com .mongodb .client .model .ValidationOptions ();
2507+ if (collectionOptions .containsKey ("capped" )) {
2508+ co .capped ((Boolean ) collectionOptions .get ("capped" ));
2509+ }
2510+ if (collectionOptions .containsKey ("size" )) {
2511+ co .sizeInBytes (((Number ) collectionOptions .get ("size" )).longValue ());
2512+ }
2513+ if (collectionOptions .containsKey ("max" )) {
2514+ co .maxDocuments (((Number ) collectionOptions .get ("max" )).longValue ());
2515+ }
25012516
2502- if (collectionOptions .containsKey ("validationLevel" )) {
2503- options .validationLevel (ValidationLevel .fromString (collectionOptions .getString ("validationLevel" )));
2504- }
2505- if (collectionOptions .containsKey ("validationAction" )) {
2506- options .validationAction (ValidationAction .fromString (collectionOptions .getString ("validationAction" )));
2507- }
2517+ if (collectionOptions .containsKey ("collation" )) {
2518+ co .collation (IndexConverters .fromDocument (collectionOptions .get ("collation" , Document .class )));
2519+ }
25082520
2509- options .validator (collectionOptions .get ("validator" , Document .class ));
2510- co .validationOptions (options );
2511- }
2521+ if (collectionOptions .containsKey ("validator" )) {
25122522
2513- if ( collectionOptions . containsKey ( "timeseries" )) {
2523+ ValidationOptions options = new ValidationOptions ();
25142524
2515- Document timeSeries = collectionOptions .get ("timeseries" , Document .class );
2516- com .mongodb .client .model .TimeSeriesOptions options = new com .mongodb .client .model .TimeSeriesOptions (
2517- timeSeries .getString ("timeField" ));
2518- if (timeSeries .containsKey ("metaField" )) {
2519- options .metaField (timeSeries .getString ("metaField" ));
2520- }
2521- if (timeSeries .containsKey ("granularity" )) {
2522- options .granularity (TimeSeriesGranularity .valueOf (timeSeries .getString ("granularity" ).toUpperCase ()));
2523- }
2524- co .timeSeriesOptions (options );
2525+ if (collectionOptions .containsKey ("validationLevel" )) {
2526+ options .validationLevel (ValidationLevel .fromString (collectionOptions .getString ("validationLevel" )));
2527+ }
2528+ if (collectionOptions .containsKey ("validationAction" )) {
2529+ options .validationAction (ValidationAction .fromString (collectionOptions .getString ("validationAction" )));
25252530 }
25262531
2527- db .createCollection (collectionName , co );
2532+ options .validator (collectionOptions .get ("validator" , Document .class ));
2533+ co .validationOptions (options );
2534+ }
25282535
2529- MongoCollection < Document > coll = db . getCollection ( collectionName , Document . class );
2536+ if ( collectionOptions . containsKey ( "timeseries" )) {
25302537
2531- // TODO: Emit a collection created event
2532- if ( LOGGER . isDebugEnabled ()) {
2533- LOGGER . debug ( String . format ( "Created collection [%s]" ,
2534- coll . getNamespace () != null ? coll . getNamespace (). getCollectionName () : collectionName ));
2538+ Document timeSeries = collectionOptions . get ( "timeseries" , Document . class );
2539+ TimeSeriesOptions options = new TimeSeriesOptions ( timeSeries . getString ( "timeField" ));
2540+ if ( timeSeries . containsKey ( "metaField" )) {
2541+ options . metaField ( timeSeries . getString ( "metaField" ));
25352542 }
2536- return coll ;
2537- });
2543+ if (timeSeries .containsKey ("granularity" )) {
2544+ options .granularity (TimeSeriesGranularity .valueOf (timeSeries .getString ("granularity" ).toUpperCase ()));
2545+ }
2546+ co .timeSeriesOptions (options );
2547+ }
2548+ return co ;
25382549 }
25392550
25402551 /**
0 commit comments