@@ -648,13 +648,8 @@ public <T> MongoCollection<Document> createCollection(Class<T> entityClass,
648
648
649
649
Assert .notNull (entityClass , "EntityClass must not be null!" );
650
650
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 ));
658
653
}
659
654
660
655
/*
@@ -676,7 +671,8 @@ public MongoCollection<Document> createCollection(String collectionName,
676
671
@ Nullable CollectionOptions collectionOptions ) {
677
672
678
673
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 ));
680
676
}
681
677
682
678
/*
@@ -2475,66 +2471,81 @@ protected <T> T maybeCallAfterConvert(T object, Document document, String collec
2475
2471
* @param collectionOptions
2476
2472
* @return the collection that was created
2477
2473
*/
2478
- @ SuppressWarnings ("ConstantConditions" )
2479
2474
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 ) {
2480
2489
return execute (db -> {
2490
+ db .createCollection (collectionName , collectionOptions );
2481
2491
2482
- CreateCollectionOptions co = new CreateCollectionOptions ( );
2492
+ MongoCollection < Document > coll = db . getCollection ( collectionName , Document . class );
2483
2493
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 ));
2492
2498
}
2499
+ return coll ;
2500
+ });
2501
+ }
2493
2502
2494
- if (collectionOptions .containsKey ("collation" )) {
2495
- co .collation (IndexConverters .fromDocument (collectionOptions .get ("collation" , Document .class )));
2496
- }
2503
+ private CreateCollectionOptions getCreateCollectionOptions (Document collectionOptions ) {
2497
2504
2498
- if ( collectionOptions . containsKey ( "validator" )) {
2505
+ CreateCollectionOptions co = new CreateCollectionOptions ();
2499
2506
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
+ }
2501
2516
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
+ }
2508
2520
2509
- options .validator (collectionOptions .get ("validator" , Document .class ));
2510
- co .validationOptions (options );
2511
- }
2521
+ if (collectionOptions .containsKey ("validator" )) {
2512
2522
2513
- if ( collectionOptions . containsKey ( "timeseries" )) {
2523
+ ValidationOptions options = new ValidationOptions ();
2514
2524
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" )));
2525
2530
}
2526
2531
2527
- db .createCollection (collectionName , co );
2532
+ options .validator (collectionOptions .get ("validator" , Document .class ));
2533
+ co .validationOptions (options );
2534
+ }
2528
2535
2529
- MongoCollection < Document > coll = db . getCollection ( collectionName , Document . class );
2536
+ if ( collectionOptions . containsKey ( "timeseries" )) {
2530
2537
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" ));
2535
2542
}
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 ;
2538
2549
}
2539
2550
2540
2551
/**
0 commit comments