|
62 | 62 | */
|
63 | 63 | public final class ReferenceLookupDelegate {
|
64 | 64 |
|
| 65 | + private static final Document NO_RESULTS_PREDICATE = new Document("_id", new Document("$exists", false)); |
| 66 | + |
65 | 67 | private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
|
66 | 68 | private final SpELContext spELContext;
|
67 | 69 | private final ParameterBindingDocumentCodec codec;
|
@@ -262,25 +264,32 @@ DocumentReferenceQuery computeFilter(MongoPersistentProperty property, Object so
|
262 | 264 | sort);
|
263 | 265 | }
|
264 | 266 |
|
265 |
| - List<Document> ors = new ArrayList<>(); |
266 |
| - for (Object entry : (Collection<Object>) value) { |
| 267 | + Collection<Object> objects = (Collection<Object>) value; |
267 | 268 |
|
268 |
| - Document decoded = codec.decode(lookup, bindingContext(property, entry, spELContext)); |
269 |
| - ors.add(decoded); |
| 269 | + if (objects.isEmpty()) { |
| 270 | + return new ListDocumentReferenceQuery(NO_RESULTS_PREDICATE, sort); |
270 | 271 | }
|
271 | 272 |
|
272 |
| - if(ors.isEmpty()) { |
273 |
| - return new ListDocumentReferenceQuery(new Document("_id", new Document("$exists", false)), sort); |
| 273 | + List<Document> ors = new ArrayList<>(objects.size()); |
| 274 | + for (Object entry : objects) { |
| 275 | + |
| 276 | + Document decoded = codec.decode(lookup, bindingContext(property, entry, spELContext)); |
| 277 | + ors.add(decoded); |
274 | 278 | }
|
275 | 279 |
|
276 | 280 | return new ListDocumentReferenceQuery(new Document("$or", ors), sort);
|
277 | 281 | }
|
278 | 282 |
|
279 | 283 | if (property.isMap() && value instanceof Map) {
|
280 | 284 |
|
281 |
| - Map<Object, Document> filterMap = new LinkedHashMap<>(); |
| 285 | + Set<Entry<Object, Object>> entries = ((Map<Object, Object>) value).entrySet(); |
| 286 | + if (entries.isEmpty()) { |
| 287 | + return new MapDocumentReferenceQuery(NO_RESULTS_PREDICATE, sort, Collections.emptyMap()); |
| 288 | + } |
| 289 | + |
| 290 | + Map<Object, Document> filterMap = new LinkedHashMap<>(entries.size()); |
282 | 291 |
|
283 |
| - for (Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) { |
| 292 | + for (Entry<Object, Object> entry : entries) { |
284 | 293 |
|
285 | 294 | Document decoded = codec.decode(lookup, bindingContext(property, entry.getValue(), spELContext));
|
286 | 295 | filterMap.put(entry.getKey(), decoded);
|
|
0 commit comments