@@ -193,12 +193,11 @@ public Document getMappedSort(Document sortObject, @Nullable MongoPersistentEnti
193
193
Assert .notNull (sortObject , "SortObject must not be null!" );
194
194
195
195
if (sortObject .isEmpty ()) {
196
- return new Document () ;
196
+ return BsonUtils . EMPTY_DOCUMENT ;
197
197
}
198
198
199
199
Document mappedSort = mapFieldsToPropertyNames (sortObject , entity );
200
- mapMetaAttributes (mappedSort , entity , MetaMapping .WHEN_PRESENT );
201
- return mappedSort ;
200
+ return mapMetaAttributes (mappedSort , entity , MetaMapping .WHEN_PRESENT );
202
201
}
203
202
204
203
/**
@@ -215,42 +214,51 @@ public Document getMappedFields(Document fieldsObject, @Nullable MongoPersistent
215
214
Assert .notNull (fieldsObject , "FieldsObject must not be null!" );
216
215
217
216
Document mappedFields = mapFieldsToPropertyNames (fieldsObject , entity );
218
- mapMetaAttributes (mappedFields , entity , MetaMapping .FORCE );
219
- return mappedFields ;
217
+ return mapMetaAttributes (mappedFields , entity , MetaMapping .FORCE );
220
218
}
221
219
222
220
private Document mapFieldsToPropertyNames (Document fields , @ Nullable MongoPersistentEntity <?> entity ) {
223
221
224
222
if (fields .isEmpty ()) {
225
- return new Document () ;
223
+ return BsonUtils . EMPTY_DOCUMENT ;
226
224
227
225
}
228
226
Document target = new Document ();
229
- for (Map .Entry <String , Object > entry : BsonUtils .asMap (filterUnwrappedObjects (fields , entity )).entrySet ()) {
230
227
231
- Field field = createPropertyField (entity , entry .getKey (), mappingContext );
228
+ BsonUtils .asMap (filterUnwrappedObjects (fields , entity )).forEach ((k , v ) -> {
229
+
230
+ Field field = createPropertyField (entity , k , mappingContext );
232
231
if (field .getProperty () != null && field .getProperty ().isUnwrapped ()) {
233
- continue ;
232
+ return ;
234
233
}
235
234
236
- target .put (field .getMappedKey (), entry .getValue ());
237
- }
235
+ target .put (field .getMappedKey (), v );
236
+ });
237
+
238
238
return target ;
239
239
}
240
240
241
- private void mapMetaAttributes (Document source , @ Nullable MongoPersistentEntity <?> entity , MetaMapping metaMapping ) {
241
+ private Document mapMetaAttributes (Document source , @ Nullable MongoPersistentEntity <?> entity ,
242
+ MetaMapping metaMapping ) {
242
243
243
244
if (entity == null ) {
244
- return ;
245
+ return source ;
245
246
}
246
247
247
248
if (entity .hasTextScoreProperty () && !MetaMapping .IGNORE .equals (metaMapping )) {
249
+
250
+ if (source == BsonUtils .EMPTY_DOCUMENT ) {
251
+ source = new Document ();
252
+ }
253
+
248
254
MongoPersistentProperty textScoreProperty = entity .getTextScoreProperty ();
249
255
if (MetaMapping .FORCE .equals (metaMapping )
250
256
|| (MetaMapping .WHEN_PRESENT .equals (metaMapping ) && source .containsKey (textScoreProperty .getFieldName ()))) {
251
257
source .putAll (getMappedTextScoreField (textScoreProperty ));
252
258
}
253
259
}
260
+
261
+ return source ;
254
262
}
255
263
256
264
private Document filterUnwrappedObjects (Document fieldsObject , @ Nullable MongoPersistentEntity <?> entity ) {
@@ -679,7 +687,7 @@ protected final Entry<String, Object> createMapEntry(Field field, @Nullable Obje
679
687
private Entry <String , Object > createMapEntry (String key , @ Nullable Object value ) {
680
688
681
689
Assert .hasText (key , "Key must not be null or empty!" );
682
- return Collections . singletonMap (key , value ). entrySet (). iterator (). next ( );
690
+ return new AbstractMap . SimpleEntry <> (key , value );
683
691
}
684
692
685
693
private Object createReferenceFor (Object source , MongoPersistentProperty property ) {
@@ -733,13 +741,13 @@ protected boolean isNestedKeyword(@Nullable Object candidate) {
733
741
return false ;
734
742
}
735
743
736
- Set <String > keys = BsonUtils .asMap ((Bson ) candidate ). keySet ( );
744
+ Map <String , Object > map = BsonUtils .asMap ((Bson ) candidate );
737
745
738
- if (keys .size () != 1 ) {
746
+ if (map .size () != 1 ) {
739
747
return false ;
740
748
}
741
749
742
- return isKeyword (keys . iterator ().next ());
750
+ return isKeyword (map . entrySet (). iterator ().next (). getKey ());
743
751
}
744
752
745
753
/**
@@ -823,11 +831,14 @@ public Keyword(Bson source, String key) {
823
831
824
832
public Keyword (Bson bson ) {
825
833
826
- Set <String > keys = BsonUtils .asMap (bson ).keySet ();
827
- Assert .isTrue (keys .size () == 1 , "Can only use a single value Document!" );
834
+ Map <String , Object > map = BsonUtils .asMap (bson );
835
+ Assert .isTrue (map .size () == 1 , "Can only use a single value Document!" );
836
+
837
+ Set <Entry <String , Object >> entries = map .entrySet ();
838
+ Entry <String , Object > entry = entries .iterator ().next ();
828
839
829
- this .key = keys . iterator (). next ();
830
- this .value = BsonUtils . get ( bson , key );
840
+ this .key = entry . getKey ();
841
+ this .value = entry . getValue ( );
831
842
}
832
843
833
844
/**
0 commit comments