From b77658e18888d4b563459753e219302e56ee3450 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 9 Jul 2018 10:34:44 +0200 Subject: [PATCH] DATAMONGO-2023 - Allow usage of $sample in aggregation pipelines. We now allow usage of $sample as aggregation framework stage and are no longer in the way with Query by Example. Previously, we identified Example objects using the $sample keyword which prevented query mapping of aggregation pipelines that contained a sample stage. We already fixed this issue via DATAMONGO-1325 for the 2.x line. Original Pull Request: #585 --- .../data/mongodb/core/convert/QueryMapper.java | 4 ++-- .../data/mongodb/core/query/Criteria.java | 4 ++-- .../mongodb/core/convert/QueryMapperUnitTests.java | 11 +++++++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java index 1b932e951..be19f0e35 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2017 the original author or authors. + * Copyright 2011-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -592,7 +592,7 @@ public class QueryMapper { * @since 1.8 */ public boolean isSample() { - return "$sample".equalsIgnoreCase(key); + return "$example".equalsIgnoreCase(key); } public boolean hasIterableValue() { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java index dd843e898..e67d6b589 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2017 the original author or authors. + * Copyright 2010-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -541,7 +541,7 @@ public class Criteria implements CriteriaDefinition { */ public Criteria alike(Example sample) { - criteria.put("$sample", sample); + criteria.put("$example", sample); this.criteriaChain.add(this); return this; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java index 07353d7e1..787d9dc92 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2017 the original author or authors. + * Copyright 2011-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; - import org.springframework.data.annotation.Id; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; @@ -140,6 +139,14 @@ public class QueryMapperUnitTests { assertThat(dbObject.get("$ne"), is(instanceOf(ObjectId.class))); } + @Test // DATAMONGO-2023 + public void translates$SampleCorrectly() { + + DBObject dbObject = new BasicDBObject("$sample", new BasicDBObject("size", 1)); + DBObject result = mapper.getMappedObject(dbObject, context.getPersistentEntity(Sample.class)); + assertThat((BasicDBObject) result.get("$sample"), is(new BasicDBObject("size", 1))); + } + @Test // DATAMONGO-326 public void handlesEnumsCorrectly() { Query query = query(where("foo").is(Enum.INSTANCE));