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
This commit is contained in:
Mark Paluch
2018-07-09 10:34:44 +02:00
committed by Christoph Strobl
parent 0cb0cb700b
commit b77658e188
3 changed files with 13 additions and 6 deletions

View File

@@ -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() {

View File

@@ -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;
}

View File

@@ -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));