Browse Source

Simplify usage of user provided aggregation operations.

Introduce Aggregation.stage which allows to use a plain JSON String or any valid Bson representation to be used within an aggregation pipeline stage, without having to implement AggregationOperation directly.
The change allows to make use of driver native builder API for aggregates.

Original pull request: #4059.
Closes #4038
pull/4093/head
Christoph Strobl 4 years ago committed by Mark Paluch
parent
commit
ee076ec02f
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 35
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java
  2. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationContext.java
  3. 61
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/BasicAggregationOperation.java
  4. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFieldsAggregationOperationContext.java
  5. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/NestedDelegatingExpressionAggregationOperationContext.java
  6. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/PrefixingDelegatingAggregationOperationContext.java
  7. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java
  8. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  9. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverter.java
  10. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  11. 55
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java
  12. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/aggregation/TestAggregationContext.java
  13. 40
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java
  14. 89
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/BasicAggregationOperationUnitTests.java
  15. 20
      src/main/asciidoc/reference/aggregation-framework.adoc

35
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

@ -21,6 +21,7 @@ import java.util.Arrays; @@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.List;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.aggregation.AddFieldsOperation.AddFieldsOperationBuilder;
@ -238,6 +239,40 @@ public class Aggregation { @@ -238,6 +239,40 @@ public class Aggregation {
return AddFieldsOperation.builder();
}
/**
* Creates a new {@link AggregationOperation} taking the given {@link Bson bson value} as is. <br />
*
* <pre class="code">
* Aggregation.stage(Aggregates.search(exists(fieldPath("..."))));
* </pre>
*
* Field mapping against a potential domain type or previous aggregation stages will not happen.
*
* @param aggregationOperation the must not be {@literal null}.
* @return new instance of {@link AggregationOperation}.
* @since 4.0
*/
public static AggregationOperation stage(Bson aggregationOperation) {
return new BasicAggregationOperation(aggregationOperation);
}
/**
* Creates a new {@link AggregationOperation} taking the given {@link String json value} as is. <br />
*
* <pre class="code">
* Aggregation.stage("{ $search : { near : { path : 'released' , origin : ... } } }");
* </pre>
*
* Field mapping against a potential domain type or previous aggregation stages will not happen.
*
* @param json the JSON representation of the pipeline stage. Must not be {@literal null}.
* @return new instance of {@link AggregationOperation}.
* @since 4.0
*/
public static AggregationOperation stage(String json) {
return new BasicAggregationOperation(json);
}
/**
* Creates a new {@link ProjectionOperation} including the given fields.
*

11
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationContext.java

@ -20,12 +20,16 @@ import java.lang.reflect.Method; @@ -20,12 +20,16 @@ import java.lang.reflect.Method;
import java.util.Arrays;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.CodecRegistryProvider;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import com.mongodb.MongoClientSettings;
/**
* The context for an {@link AggregationOperation}.
*
@ -33,7 +37,7 @@ import org.springframework.util.ReflectionUtils; @@ -33,7 +37,7 @@ import org.springframework.util.ReflectionUtils;
* @author Christoph Strobl
* @since 1.3
*/
public interface AggregationOperationContext {
public interface AggregationOperationContext extends CodecRegistryProvider {
/**
* Returns the mapped {@link Document}, potentially converting the source considering mapping metadata etc.
@ -114,4 +118,9 @@ public interface AggregationOperationContext { @@ -114,4 +118,9 @@ public interface AggregationOperationContext {
default AggregationOperationContext continueOnMissingFieldReference() {
return this;
}
@Override
default CodecRegistry getCodecRegistry() {
return MongoClientSettings.getDefaultCodecRegistry();
}
}

61
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/BasicAggregationOperation.java

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.aggregation;
import java.util.Map;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.data.mongodb.util.BsonUtils;
import org.springframework.util.ObjectUtils;
/**
* {@link AggregationOperation} implementation that c
*
* @author Christoph Strobl
* @since 4.0
*/
class BasicAggregationOperation implements AggregationOperation {
private final Object value;
BasicAggregationOperation(Object value) {
this.value = value;
}
@Override
public Document toDocument(AggregationOperationContext context) {
if (value instanceof Document document) {
return document;
}
if (value instanceof Bson bson) {
return BsonUtils.asDocument(bson, context.getCodecRegistry());
}
if (value instanceof Map map) {
return new Document(map);
}
if (value instanceof String json && BsonUtils.isJsonDocument(json)) {
return BsonUtils.parse(json, context);
}
throw new IllegalStateException(
String.format("%s cannot be converted to org.bson.Document.", ObjectUtils.nullSafeClassName(value)));
}
}

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFieldsAggregationOperationContext.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.data.mongodb.core.aggregation;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
@ -141,4 +142,9 @@ class ExposedFieldsAggregationOperationContext implements AggregationOperationCo @@ -141,4 +142,9 @@ class ExposedFieldsAggregationOperationContext implements AggregationOperationCo
AggregationOperationContext getRootContext() {
return rootContext;
}
@Override
public CodecRegistry getCodecRegistry() {
return getRootContext().getCodecRegistry();
}
}

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/NestedDelegatingExpressionAggregationOperationContext.java

@ -19,6 +19,7 @@ import java.util.Collection; @@ -19,6 +19,7 @@ import java.util.Collection;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExpressionFieldReference;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.util.Assert;
@ -92,4 +93,9 @@ class NestedDelegatingExpressionAggregationOperationContext implements Aggregati @@ -92,4 +93,9 @@ class NestedDelegatingExpressionAggregationOperationContext implements Aggregati
public Fields getFields(Class<?> type) {
return delegate.getFields(type);
}
@Override
public CodecRegistry getCodecRegistry() {
return delegate.getCodecRegistry();
}
}

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/PrefixingDelegatingAggregationOperationContext.java

@ -24,6 +24,7 @@ import java.util.Map; @@ -24,6 +24,7 @@ import java.util.Map;
import java.util.Set;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.lang.Nullable;
@ -80,6 +81,11 @@ public class PrefixingDelegatingAggregationOperationContext implements Aggregati @@ -80,6 +81,11 @@ public class PrefixingDelegatingAggregationOperationContext implements Aggregati
return delegate.getFields(type);
}
@Override
public CodecRegistry getCodecRegistry() {
return delegate.getCodecRegistry();
}
@SuppressWarnings("unchecked")
private Document doPrefix(Document source) {

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java

@ -22,6 +22,7 @@ import java.util.List; @@ -22,6 +22,7 @@ import java.util.List;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference;
@ -147,4 +148,9 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio @@ -147,4 +148,9 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio
public Class<?> getType() {
return type;
}
@Override
public CodecRegistry getCodecRegistry() {
return this.mapper.getConverter().getCodecRegistry();
}
}

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFactory; @@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFactory;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.json.JsonReader;
import org.bson.types.ObjectId;
@ -1793,6 +1794,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App @@ -1793,6 +1794,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return conversions.getCustomWriteTarget(source).orElse(source);
}
@Override
public CodecRegistry getCodecRegistry() {
return codecRegistryProvider != null ? codecRegistryProvider.getCodecRegistry() : super.getCodecRegistry();
}
/**
* Create a new {@link MappingMongoConverter} using the given {@link MongoDatabaseFactory} when loading {@link DBRef}.
*

11
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverter.java

@ -15,16 +15,18 @@ @@ -15,16 +15,18 @@
*/
package org.springframework.data.mongodb.core.convert;
import com.mongodb.MongoClientSettings;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.springframework.core.convert.ConversionException;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.convert.EntityConverter;
import org.springframework.data.convert.EntityReader;
import org.springframework.data.convert.TypeMapper;
import org.springframework.data.mongodb.CodecRegistryProvider;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.util.BsonUtils;
@ -48,7 +50,7 @@ import com.mongodb.DBRef; @@ -48,7 +50,7 @@ import com.mongodb.DBRef;
*/
public interface MongoConverter
extends EntityConverter<MongoPersistentEntity<?>, MongoPersistentProperty, Object, Bson>, MongoWriter<Object>,
EntityReader<Object, Bson> {
EntityReader<Object, Bson>, CodecRegistryProvider {
/**
* Returns the {@link TypeMapper} being used to write type information into {@link Document}s created with that
@ -188,4 +190,9 @@ public interface MongoConverter @@ -188,4 +190,9 @@ public interface MongoConverter
}
}
@Override
default CodecRegistry getCodecRegistry() {
return MongoClientSettings.getDefaultCodecRegistry();
}
}

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

@ -1486,4 +1486,8 @@ public class QueryMapper { @@ -1486,4 +1486,8 @@ public class QueryMapper {
public MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> getMappingContext() {
return mappingContext;
}
public MongoConverter getConverter() {
return converter;
}
}

55
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java

@ -35,6 +35,7 @@ import org.bson.BsonString; @@ -35,6 +35,7 @@ import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.json.JsonParseException;
import org.bson.types.ObjectId;
@ -81,18 +82,36 @@ public class BsonUtils { @@ -81,18 +82,36 @@ public class BsonUtils {
* @return
*/
public static Map<String, Object> asMap(Bson bson) {
return asMap(bson, MongoClientSettings.getDefaultCodecRegistry());
}
if (bson instanceof Document) {
return (Document) bson;
/**
* Return the {@link Bson} object as {@link Map}. Depending on the input type, the return value can be either a casted
* version of {@code bson} or a converted (detached from the original value) using the given {@link CodecRegistry} to
* obtain {@link org.bson.codecs.Codec codecs} that might be required for conversion.
*
* @param bson can be {@literal null}.
* @param codecRegistry must not be {@literal null}.
* @return never {@literal null}. Returns an empty {@link Map} if input {@link Bson} is {@literal null}.
* @since 4.0
*/
public static Map<String, Object> asMap(@Nullable Bson bson, CodecRegistry codecRegistry) {
if (bson == null) {
return Collections.emptyMap();
}
if (bson instanceof BasicDBObject) {
return ((BasicDBObject) bson);
if (bson instanceof Document document) {
return document;
}
if (bson instanceof DBObject) {
return ((DBObject) bson).toMap();
if (bson instanceof BasicDBObject dbo) {
return dbo;
}
if (bson instanceof DBObject dbo) {
return dbo.toMap();
}
return (Map) bson.toBsonDocument(Document.class, MongoClientSettings.getDefaultCodecRegistry());
return new Document((Map) bson.toBsonDocument(Document.class, codecRegistry));
}
/**
@ -104,12 +123,26 @@ public class BsonUtils { @@ -104,12 +123,26 @@ public class BsonUtils {
* @since 3.2.5
*/
public static Document asDocument(Bson bson) {
return asDocument(bson, MongoClientSettings.getDefaultCodecRegistry());
}
if (bson instanceof Document) {
return (Document) bson;
/**
* Return the {@link Bson} object as {@link Document}. Depending on the input type, the return value can be either a
* casted version of {@code bson} or a converted (detached from the original value) using the given
* {@link CodecRegistry} to obtain {@link org.bson.codecs.Codec codecs} that might be required for conversion.
*
* @param bson
* @param codecRegistry must not be {@literal null}.
* @return never {@literal null}.
* @since 4.0
*/
public static Document asDocument(Bson bson, CodecRegistry codecRegistry) {
if (bson instanceof Document document) {
return document;
}
Map<String, Object> map = asMap(bson);
Map<String, Object> map = asMap(bson, codecRegistry);
if (map instanceof Document) {
return (Document) map;
@ -413,7 +446,7 @@ public class BsonUtils { @@ -413,7 +446,7 @@ public class BsonUtils {
*/
public static boolean isJsonDocument(@Nullable String value) {
if(!StringUtils.hasText(value)) {
if (!StringUtils.hasText(value)) {
return false;
}

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/aggregation/TestAggregationContext.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.data.mongodb.util.aggregation;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
@ -72,4 +73,9 @@ public class TestAggregationContext implements AggregationOperationContext { @@ -72,4 +73,9 @@ public class TestAggregationContext implements AggregationOperationContext {
public FieldReference getReference(String name) {
return delegate.getReference(name);
}
@Override
public CodecRegistry getCodecRegistry() {
return delegate.getCodecRegistry();
}
}

40
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java

@ -24,6 +24,7 @@ import java.util.ArrayList; @@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.junit.jupiter.api.Test;
@ -37,6 +38,9 @@ import org.springframework.data.mongodb.core.convert.QueryMapper; @@ -37,6 +38,9 @@ import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.query.Criteria;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Projections;
/**
* Unit tests for {@link Aggregation}.
*
@ -599,31 +603,51 @@ public class AggregationUnitTests { @@ -599,31 +603,51 @@ public class AggregationUnitTests {
assertThat(extractPipelineElement(target, 1, "$project")).isEqualTo(Document.parse(" { \"_id\" : \"$_id\" }"));
}
@Test // GH-3898
void shouldNotConvertIncludeExcludeValuesForProjectOperation() {
MongoMappingContext mappingContext = new MongoMappingContext();
RelaxedTypeBasedAggregationOperationContext context = new RelaxedTypeBasedAggregationOperationContext(WithRetypedIdField.class, mappingContext,
RelaxedTypeBasedAggregationOperationContext context = new RelaxedTypeBasedAggregationOperationContext(
WithRetypedIdField.class, mappingContext,
new QueryMapper(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext)));
Document document = project(WithRetypedIdField.class).toDocument(context);
assertThat(document).isEqualTo(new Document("$project", new Document("_id", 1).append("renamed-field", 1)));
}
@Test // GH-4038
void createsBasicAggregationOperationFromJsonString() {
AggregationOperation stage = stage("{ $project : { name : 1} }");
Document target = newAggregation(stage).toDocument("col-1", DEFAULT_CONTEXT);
assertThat(extractPipelineElement(target, 0, "$project")).containsEntry("name", 1);
}
@Test // GH-4038
void createsBasicAggregationOperationFromBson() {
AggregationOperation stage = stage(Aggregates.project(Projections.fields(Projections.include("name"))));
Document target = newAggregation(stage).toDocument("col-1", DEFAULT_CONTEXT);
assertThat(extractPipelineElement(target, 0, "$project")).containsKey("name");
}
private Document extractPipelineElement(Document agg, int index, String operation) {
List<Document> pipeline = (List<Document>) agg.get("pipeline");
return (Document) pipeline.get(index).get(operation);
Object value = pipeline.get(index).get(operation);
if (value instanceof Document document) {
return document;
}
if (value instanceof Map map) {
return new Document(map);
}
throw new IllegalArgumentException();
}
public class WithRetypedIdField {
@Id
@org.springframework.data.mongodb.core.mapping.Field
private String id;
@Id @org.springframework.data.mongodb.core.mapping.Field private String id;
@org.springframework.data.mongodb.core.mapping.Field("renamed-field")
private String foo;
@org.springframework.data.mongodb.core.mapping.Field("renamed-field") private String foo;
}
}

89
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/BasicAggregationOperationUnitTests.java

@ -0,0 +1,89 @@ @@ -0,0 +1,89 @@
/*
* Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.aggregation;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import org.bson.Document;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import com.mongodb.MongoClientSettings;
/**
* @author Christoph Strobl
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class BasicAggregationOperationUnitTests {
@Mock QueryMapper queryMapper;
@Mock MongoConverter converter;
TypeBasedAggregationOperationContext ctx;
@BeforeEach
void beforeEach() {
// no field mapping though having a type based context
ctx = new TypeBasedAggregationOperationContext(Person.class, new MongoMappingContext(), queryMapper);
when(queryMapper.getConverter()).thenReturn(converter);
when(converter.getCodecRegistry()).thenReturn(MongoClientSettings.getDefaultCodecRegistry());
}
@Test // GH-4038
void usesGivenDocumentAsIs() {
Document source = new Document("value", 1);
assertThat(new BasicAggregationOperation(source).toDocument(ctx)).isSameAs(source);
}
@Test // GH-4038
void parsesJson() {
Document source = new Document("value", 1);
assertThat(new BasicAggregationOperation(source.toJson()).toDocument(ctx)).isEqualTo(source);
}
@Test // GH-4038
void errorsOnInvalidValue() {
BasicAggregationOperation agg = new BasicAggregationOperation(new Object());
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> agg.toDocument(ctx));
}
@Test // GH-4038
void errorsOnNonJsonSting() {
BasicAggregationOperation agg = new BasicAggregationOperation("#005BBB #FFD500");
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> agg.toDocument(ctx));
}
private static class Person {
@Field("v-a-l-u-e") Object value;
}
}

20
src/main/asciidoc/reference/aggregation-framework.adoc

@ -126,6 +126,26 @@ At the time of this writing, we provide support for the following Aggregation Op @@ -126,6 +126,26 @@ At the time of this writing, we provide support for the following Aggregation Op
Note that the aggregation operations not listed here are currently not supported by Spring Data MongoDB. Comparison aggregation operators are expressed as `Criteria` expressions.
[TIP]
====
Unsupported aggregation operations/operators can be provided by implementing either `AggregationOperation` or `AggregationExpression`.
`Aggregation.stage` is a shortcut for registering a pipeline stage by providing its JSON or `Bson` representation.
[source,java]
----
Aggregation.stage("""
{ $search : {
"near": {
"path": "released",
"origin": { "$date": { "$numberLong": "..." } } ,
"pivot": 7
}
}
}
""");
----
====
[[mongo.aggregation.projection]]
=== Projection Expressions

Loading…
Cancel
Save