@@ -437,7 +437,7 @@ public <T> CloseableIterator<T> stream(Query query, Class<T> entityType, String
437
437
}
438
438
439
439
@ SuppressWarnings ("ConstantConditions" )
440
- protected <T > CloseableIterator <T > doStream (Query query , final Class <?> entityType , String collectionName ,
440
+ protected <T > CloseableIterator <T > doStream (Query query , Class <?> entityType , String collectionName ,
441
441
Class <T > returnType ) {
442
442
443
443
Assert .notNull (query , "Query must not be null!" );
@@ -471,7 +471,7 @@ public String getCollectionName(Class<?> entityClass) {
471
471
*/
472
472
@ Override
473
473
@ SuppressWarnings ("ConstantConditions" )
474
- public Document executeCommand (final String jsonCommand ) {
474
+ public Document executeCommand (String jsonCommand ) {
475
475
476
476
Assert .hasText (jsonCommand , "JsonCommand must not be null nor empty!" );
477
477
@@ -484,7 +484,7 @@ public Document executeCommand(final String jsonCommand) {
484
484
*/
485
485
@ Override
486
486
@ SuppressWarnings ("ConstantConditions" )
487
- public Document executeCommand (final Document command ) {
487
+ public Document executeCommand (Document command ) {
488
488
489
489
Assert .notNull (command , "Command must not be null!" );
490
490
@@ -655,7 +655,7 @@ public <T> MongoCollection<Document> createCollection(Class<T> entityClass,
655
655
* (non-Javadoc)
656
656
* @see org.springframework.data.mongodb.core.MongoOperations#createCollection(java.lang.String)
657
657
*/
658
- public MongoCollection <Document > createCollection (final String collectionName ) {
658
+ public MongoCollection <Document > createCollection (String collectionName ) {
659
659
660
660
Assert .notNull (collectionName , "CollectionName must not be null!" );
661
661
@@ -666,8 +666,8 @@ public MongoCollection<Document> createCollection(final String collectionName) {
666
666
* (non-Javadoc)
667
667
* @see org.springframework.data.mongodb.core.MongoOperations#createCollection(java.lang.String, org.springframework.data.mongodb.core.CollectionOptions)
668
668
*/
669
- public MongoCollection <Document > createCollection (final String collectionName ,
670
- final @ Nullable CollectionOptions collectionOptions ) {
669
+ public MongoCollection <Document > createCollection (String collectionName ,
670
+ @ Nullable CollectionOptions collectionOptions ) {
671
671
672
672
Assert .notNull (collectionName , "CollectionName must not be null!" );
673
673
return doCreateCollection (collectionName , convertToDocument (collectionOptions ));
@@ -678,7 +678,7 @@ public MongoCollection<Document> createCollection(final String collectionName,
678
678
* @see org.springframework.data.mongodb.core.MongoOperations#getCollection(java.lang.String)
679
679
*/
680
680
@ SuppressWarnings ("ConstantConditions" )
681
- public MongoCollection <Document > getCollection (final String collectionName ) {
681
+ public MongoCollection <Document > getCollection (String collectionName ) {
682
682
683
683
Assert .notNull (collectionName , "CollectionName must not be null!" );
684
684
@@ -698,7 +698,7 @@ public <T> boolean collectionExists(Class<T> entityClass) {
698
698
* @see org.springframework.data.mongodb.core.ExecutableInsertOperation#getCollection(java.lang.String)
699
699
*/
700
700
@ SuppressWarnings ("ConstantConditions" )
701
- public boolean collectionExists (final String collectionName ) {
701
+ public boolean collectionExists (String collectionName ) {
702
702
703
703
Assert .notNull (collectionName , "CollectionName must not be null!" );
704
704
@@ -729,15 +729,13 @@ public void dropCollection(String collectionName) {
729
729
730
730
Assert .notNull (collectionName , "CollectionName must not be null!" );
731
731
732
- execute (collectionName , new CollectionCallback <Void >() {
733
- public Void doInCollection (MongoCollection <Document > collection ) throws MongoException , DataAccessException {
734
- collection .drop ();
735
- if (LOGGER .isDebugEnabled ()) {
736
- LOGGER .debug ("Dropped collection [{}]" ,
737
- collection .getNamespace () != null ? collection .getNamespace ().getCollectionName () : collectionName );
738
- }
739
- return null ;
732
+ execute (collectionName , (CollectionCallback <Void >) collection -> {
733
+ collection .drop ();
734
+ if (LOGGER .isDebugEnabled ()) {
735
+ LOGGER .debug ("Dropped collection [{}]" ,
736
+ collection .getNamespace () != null ? collection .getNamespace ().getCollectionName () : collectionName );
740
737
}
738
+ return null ;
741
739
});
742
740
}
743
741
@@ -1154,7 +1152,7 @@ public long count(Query query, Class<?> entityClass) {
1154
1152
}
1155
1153
1156
1154
@ Override
1157
- public long count (final Query query , String collectionName ) {
1155
+ public long count (Query query , String collectionName ) {
1158
1156
return count (query , null , collectionName );
1159
1157
}
1160
1158
@@ -1484,7 +1482,7 @@ protected <T> T doSave(String collectionName, T objectToSave, MongoWriter<T> wri
1484
1482
}
1485
1483
1486
1484
@ SuppressWarnings ("ConstantConditions" )
1487
- protected Object insertDocument (final String collectionName , final Document document , final Class <?> entityClass ) {
1485
+ protected Object insertDocument (String collectionName , Document document , Class <?> entityClass ) {
1488
1486
1489
1487
if (LOGGER .isDebugEnabled ()) {
1490
1488
LOGGER .debug ("Inserting Document containing fields: {} in collection: {}" , document .keySet (), collectionName );
@@ -1505,7 +1503,7 @@ protected Object insertDocument(final String collectionName, final Document docu
1505
1503
});
1506
1504
}
1507
1505
1508
- protected List <Object > insertDocumentList (final String collectionName , final List <Document > documents ) {
1506
+ protected List <Object > insertDocumentList (String collectionName , List <Document > documents ) {
1509
1507
1510
1508
if (documents .isEmpty ()) {
1511
1509
return Collections .emptyList ();
@@ -1533,34 +1531,32 @@ protected List<Object> insertDocumentList(final String collectionName, final Lis
1533
1531
return MappedDocument .toIds (documents );
1534
1532
}
1535
1533
1536
- protected Object saveDocument (final String collectionName , final Document dbDoc , final Class <?> entityClass ) {
1534
+ protected Object saveDocument (String collectionName , Document dbDoc , Class <?> entityClass ) {
1537
1535
1538
1536
if (LOGGER .isDebugEnabled ()) {
1539
1537
LOGGER .debug ("Saving Document containing fields: {}" , dbDoc .keySet ());
1540
1538
}
1541
1539
1542
- return execute (collectionName , new CollectionCallback <Object >() {
1543
- public Object doInCollection (MongoCollection <Document > collection ) throws MongoException , DataAccessException {
1544
- MongoAction mongoAction = new MongoAction (writeConcern , MongoActionOperation .SAVE , collectionName , entityClass ,
1545
- dbDoc , null );
1546
- WriteConcern writeConcernToUse = prepareWriteConcern (mongoAction );
1540
+ return execute (collectionName , collection -> {
1541
+ MongoAction mongoAction = new MongoAction (writeConcern , MongoActionOperation .SAVE , collectionName , entityClass ,
1542
+ dbDoc , null );
1543
+ WriteConcern writeConcernToUse = prepareWriteConcern (mongoAction );
1547
1544
1548
- MappedDocument mapped = MappedDocument .of (dbDoc );
1545
+ MappedDocument mapped = MappedDocument .of (dbDoc );
1549
1546
1550
- if (!mapped .hasId ()) {
1551
- if (writeConcernToUse == null ) {
1552
- collection .insertOne (dbDoc );
1553
- } else {
1554
- collection .withWriteConcern (writeConcernToUse ).insertOne (dbDoc );
1555
- }
1556
- } else if (writeConcernToUse == null ) {
1557
- collection .replaceOne (mapped .getIdFilter (), dbDoc , new ReplaceOptions ().upsert (true ));
1547
+ if (!mapped .hasId ()) {
1548
+ if (writeConcernToUse == null ) {
1549
+ collection .insertOne (dbDoc );
1558
1550
} else {
1559
- collection .withWriteConcern (writeConcernToUse ).replaceOne (mapped .getIdFilter (), dbDoc ,
1560
- new ReplaceOptions ().upsert (true ));
1551
+ collection .withWriteConcern (writeConcernToUse ).insertOne (dbDoc );
1561
1552
}
1562
- return mapped .getId ();
1553
+ } else if (writeConcernToUse == null ) {
1554
+ collection .replaceOne (mapped .getIdFilter (), dbDoc , new ReplaceOptions ().upsert (true ));
1555
+ } else {
1556
+ collection .withWriteConcern (writeConcernToUse ).replaceOne (mapped .getIdFilter (), dbDoc ,
1557
+ new ReplaceOptions ().upsert (true ));
1563
1558
}
1559
+ return mapped .getId ();
1564
1560
});
1565
1561
}
1566
1562
@@ -1588,7 +1584,7 @@ public UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> e
1588
1584
}
1589
1585
1590
1586
@ Override
1591
- public UpdateResult updateFirst (final Query query , final UpdateDefinition update , final String collectionName ) {
1587
+ public UpdateResult updateFirst (Query query , UpdateDefinition update , String collectionName ) {
1592
1588
return doUpdate (collectionName , query , update , null , false , false );
1593
1589
}
1594
1590
@@ -1606,22 +1602,21 @@ public UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> e
1606
1602
}
1607
1603
1608
1604
@ Override
1609
- public UpdateResult updateMulti (final Query query , final UpdateDefinition update , String collectionName ) {
1605
+ public UpdateResult updateMulti (Query query , UpdateDefinition update , String collectionName ) {
1610
1606
return doUpdate (collectionName , query , update , null , false , true );
1611
1607
}
1612
1608
1613
1609
@ Override
1614
- public UpdateResult updateMulti (final Query query , final UpdateDefinition update , Class <?> entityClass ,
1615
- String collectionName ) {
1610
+ public UpdateResult updateMulti (Query query , UpdateDefinition update , Class <?> entityClass , String collectionName ) {
1616
1611
1617
1612
Assert .notNull (entityClass , "EntityClass must not be null!" );
1618
1613
1619
1614
return doUpdate (collectionName , query , update , entityClass , false , true );
1620
1615
}
1621
1616
1622
1617
@ SuppressWarnings ("ConstantConditions" )
1623
- protected UpdateResult doUpdate (final String collectionName , final Query query , final UpdateDefinition update ,
1624
- @ Nullable final Class <?> entityClass , final boolean upsert , final boolean multi ) {
1618
+ protected UpdateResult doUpdate (String collectionName , Query query , UpdateDefinition update ,
1619
+ @ Nullable Class <?> entityClass , boolean upsert , boolean multi ) {
1625
1620
1626
1621
Assert .notNull (collectionName , "CollectionName must not be null!" );
1627
1622
Assert .notNull (query , "Query must not be null!" );
@@ -1659,22 +1654,23 @@ protected UpdateResult doUpdate(final String collectionName, final Query query,
1659
1654
? new RelaxedTypeBasedAggregationOperationContext (entityClass , mappingContext , queryMapper )
1660
1655
: Aggregation .DEFAULT_CONTEXT ;
1661
1656
1662
- AggregationUpdate aUppdate = (( AggregationUpdate ) update );
1663
- List < Document > pipeline = new AggregationUtil ( queryMapper , mappingContext ) .createPipeline (aUppdate , context );
1657
+ List < Document > pipeline = new AggregationUtil ( queryMapper , mappingContext )
1658
+ .createPipeline (( AggregationUpdate ) update , context );
1664
1659
1665
1660
return execute (collectionName , collection -> {
1666
1661
1662
+ if (LOGGER .isDebugEnabled ()) {
1663
+ LOGGER .debug ("Calling update using query: {} and update: {} in collection: {}" ,
1664
+ serializeToJsonSafely (queryObj ), serializeToJsonSafely (pipeline ), collectionName );
1665
+ }
1666
+
1667
1667
MongoAction mongoAction = new MongoAction (writeConcern , MongoActionOperation .UPDATE , collectionName ,
1668
1668
entityClass , update .getUpdateObject (), queryObj );
1669
1669
WriteConcern writeConcernToUse = prepareWriteConcern (mongoAction );
1670
1670
1671
1671
collection = writeConcernToUse != null ? collection .withWriteConcern (writeConcernToUse ) : collection ;
1672
1672
1673
- if (multi ) {
1674
- return collection .updateMany (queryObj , pipeline , opts );
1675
- }
1676
-
1677
- return collection .updateOne (queryObj , pipeline , opts );
1673
+ return multi ? collection .updateMany (queryObj , pipeline , opts ) : collection .updateOne (queryObj , pipeline , opts );
1678
1674
});
1679
1675
}
1680
1676
@@ -1707,11 +1703,8 @@ protected UpdateResult doUpdate(final String collectionName, final Query query,
1707
1703
1708
1704
return collection .replaceOne (queryObj , updateObj , replaceOptions );
1709
1705
} else {
1710
- if (multi ) {
1711
- return collection .updateMany (queryObj , updateObj , opts );
1712
- } else {
1713
- return collection .updateOne (queryObj , updateObj , opts );
1714
- }
1706
+ return multi ? collection .updateMany (queryObj , updateObj , opts )
1707
+ : collection .updateOne (queryObj , updateObj , opts );
1715
1708
}
1716
1709
});
1717
1710
}
@@ -1764,14 +1757,14 @@ public DeleteResult remove(Query query, Class<?> entityClass, String collectionN
1764
1757
}
1765
1758
1766
1759
@ SuppressWarnings ("ConstantConditions" )
1767
- protected <T > DeleteResult doRemove (final String collectionName , final Query query ,
1768
- @ Nullable final Class < T > entityClass , boolean multi ) {
1760
+ protected <T > DeleteResult doRemove (String collectionName , Query query , @ Nullable Class < T > entityClass ,
1761
+ boolean multi ) {
1769
1762
1770
1763
Assert .notNull (query , "Query must not be null!" );
1771
1764
Assert .hasText (collectionName , "Collection name must not be null or empty!" );
1772
1765
1773
- final MongoPersistentEntity <?> entity = getPersistentEntity (entityClass );
1774
- final Document queryObject = queryMapper .getMappedObject (query .getQueryObject (), entity );
1766
+ MongoPersistentEntity <?> entity = getPersistentEntity (entityClass );
1767
+ Document queryObject = queryMapper .getMappedObject (query .getQueryObject (), entity );
1775
1768
1776
1769
return execute (collectionName , collection -> {
1777
1770
@@ -2693,7 +2686,7 @@ protected <T> T doFindAndModify(String collectionName, Document query, Document
2693
2686
2694
2687
Document mappedQuery = queryMapper .getMappedObject (query , entity );
2695
2688
2696
- Object mappedUpdate = new Document () ;
2689
+ Object mappedUpdate ;
2697
2690
if (update instanceof AggregationUpdate ) {
2698
2691
2699
2692
AggregationOperationContext context = entityClass != null
@@ -3114,7 +3107,8 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
3114
3107
} else if (update instanceof List ) {
3115
3108
return collection .findOneAndUpdate (query , (List <Document >) update , opts );
3116
3109
}
3117
- throw new IllegalArgumentException ("doh - that does not work" );
3110
+
3111
+ throw new IllegalArgumentException (String .format ("Using %s is not supported in findOneAndUpdate" , update ));
3118
3112
}
3119
3113
}
3120
3114
0 commit comments