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