Compare commits

...

5 Commits

Author SHA1 Message Date
Oliver Gierke
ff1703f7c9 DATAMONGO-2033 - Release version 2.1 RC2 (Lovelace). 2018-08-20 10:40:11 +02:00
Oliver Gierke
7b23f8eee2 DATAMONGO-2033 - Prepare 2.1 RC2 (Lovelace). 2018-08-20 10:39:43 +02:00
Oliver Gierke
cc97c5a961 DATAMONGO-2033 - Updated changelog. 2018-08-20 10:39:34 +02:00
Christoph Strobl
08a57e58fd DATAMONGO-2052 - Add support for $arrayToObject and $objectToArray aggregation operators.
Original pull request: #603.
2018-08-17 17:33:16 +02:00
Mark Paluch
9d27d2ff8e DATAMONGO-2059 - Document count helper restrictions for geo commands inside of transactions. 2018-08-17 17:23:53 +02:00
13 changed files with 259 additions and 14 deletions

10
pom.xml
View File

@@ -5,7 +5,7 @@
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.RC2</version>
<packaging>pom</packaging>
<name>Spring Data MongoDB</name>
@@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data.build</groupId>
<artifactId>spring-data-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.RC2</version>
</parent>
<modules>
@@ -27,7 +27,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>2.1.0.BUILD-SNAPSHOT</springdata.commons>
<springdata.commons>2.1.0.RC2</springdata.commons>
<mongo>3.8.0</mongo>
<mongo.reactivestreams>1.9.0</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
@@ -151,8 +151,8 @@
<repositories>
<repository>
<id>spring-libs-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
<id>spring-libs-milestone</id>
<url>https://repo.spring.io/libs-milestone</url>
</repository>
</repositories>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.RC2</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.RC2</version>
<relativePath>../pom.xml</relativePath>
</parent>
@@ -50,7 +50,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.RC2</version>
</dependency>
<!-- reactive -->

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.RC2</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.RC2</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@@ -267,6 +267,19 @@ public class ArrayOperators {
return (usesFieldRef() ? In.arrayOf(fieldReference) : In.arrayOf(expression)).containsValue(value);
}
/**
* Creates new {@link AggregationExpression} that converts the associated expression into an object.
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @return new instance of {@link ArrayToObject}.
* @since 2.1
*/
public ArrayToObject toObject() {
return usesFieldRef() ? ArrayToObject.arrayValueOfToObject(fieldReference)
: ArrayToObject.arrayValueOfToObject(expression);
}
/**
* @author Christoph Strobl
*/
@@ -1497,4 +1510,59 @@ public class ArrayOperators {
In containsValue(Object value);
}
}
/**
* {@link AggregationExpression} for {@code $arrayToObject} that transforms an array into a single document. <br />
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @author Christoph Strobl
* @see <a href=
* "https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/">https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/</a>
* @since 2.1
*/
public static class ArrayToObject extends AbstractAggregationExpression {
private ArrayToObject(Object value) {
super(value);
}
/**
* Converts the given array (e.g. an array of two-element arrays, a field reference to an array,...) to an object.
*
* @param array must not be {@literal null}.
* @return new instance of {@link ArrayToObject}.
*/
public static ArrayToObject arrayToObject(Object array) {
return new ArrayToObject(array);
}
/**
* Converts the array pointed to by the given {@link Field field reference} to an object.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link ArrayToObject}.
*/
public static ArrayToObject arrayValueOfToObject(String fieldReference) {
return new ArrayToObject(Fields.field(fieldReference));
}
/**
* Converts the result array of the given {@link AggregationExpression expression} to an object.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ArrayToObject}.
*/
public static ArrayToObject arrayValueOfToObject(AggregationExpression expression) {
return new ArrayToObject(expression);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
*/
@Override
protected String getMongoMethod() {
return "$arrayToObject";
}
}
}

View File

@@ -113,6 +113,17 @@ public class ObjectOperators {
public MergeObjects mergeWithValuesOf(AggregationExpression... expression) {
return merge().mergeWithValuesOf(expression);
}
/**
* Creates new {@link ObjectToArray aggregation expression} that takes the associated value and converts it to an
* array of {@link Document documents} that contain two fields {@literal k} and {@literal v} each. <br />
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @since 2.1
*/
public ObjectToArray toArray() {
return ObjectToArray.toArray(value);
}
}
/**
@@ -226,4 +237,62 @@ public class ObjectOperators {
return "$mergeObjects";
}
}
/**
* {@link AggregationExpression} for {@code $objectToArray} that converts a document to an array of {@link Document
* documents} that each contains two fields {@literal k} and {@literal v}. <br />
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @author Christoph Strobl
* @see <a href=
* "https://docs.mongodb.com/manual/reference/operator/aggregation/objectToArray/">https://docs.mongodb.com/manual/reference/operator/aggregation/objectToArray/</a>
* @since 2.1
*/
public static class ObjectToArray extends AbstractAggregationExpression {
private ObjectToArray(Object value) {
super(value);
}
/**
* Creates new {@link ObjectToArray aggregation expression} that takes the value pointed to by given {@link Field
* fieldReference} and converts it to an array.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link ObjectToArray}.
*/
public static ObjectToArray valueOfToArray(String fieldReference) {
return toArray(Fields.field(fieldReference));
}
/**
* Creates new {@link ObjectToArray aggregation expression} that takes the result value of the given
* {@link AggregationExpression expression} and converts it to an array.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ObjectToArray}.
*/
public static ObjectToArray valueOfToArray(AggregationExpression expression) {
return toArray(expression);
}
/**
* Creates new {@link ObjectToArray aggregation expression} that takes the given value and converts it to an array.
*
* @param value must not be {@literal null}.
* @return new instance of {@link ObjectToArray}.
*/
public static ObjectToArray toArray(Object value) {
return new ObjectToArray(value);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
*/
@Override
protected String getMongoMethod() {
return "$objectToArray";
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bson.Document;
import org.junit.Test;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
/**
* Unit tests for {@link ArrayOperators}
*
* @author Christoph Strobl
* @currentRead Royal Assassin - Robin Hobb
*/
public class ArrayOperatorsUnitTests {
static final String EXPRESSION_STRING = "{ \"$stablemaster\" : \"burrich\" }";
static final Document EXPRESSION_DOC = Document.parse(EXPRESSION_STRING);
static final AggregationExpression EXPRESSION = context -> EXPRESSION_DOC;
@Test // DATAMONGO-2052
public void toArrayWithFieldReference() {
assertThat(ArrayOperators.arrayOf("regal").toObject().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(Document.parse("{ $arrayToObject: \"$regal\" } "));
}
@Test // DATAMONGO-2052
public void toArrayWithExpression() {
assertThat(ArrayOperators.arrayOf(EXPRESSION).toObject().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(Document.parse("{ $arrayToObject: " + EXPRESSION_STRING + "} "));
}
@Test // DATAMONGO-2052
public void toArrayWithArgumentList() {
List<List<String>> source = new ArrayList<>();
source.add(Arrays.asList("king", "shrewd"));
source.add(Arrays.asList("prince", "verity"));
assertThat(ArrayToObject.arrayToObject(source).toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(Document.parse("{ $arrayToObject: [ [ \"king\", \"shrewd\"], [ \"prince\", \"verity\" ] ] } "));
}
}

View File

@@ -90,4 +90,18 @@ public class ObjectOperatorsUnitTests {
"{ $mergeObjects: [ \"$kettricken\", " + EXPRESSION_STRING + ", { \"fitz\" : \"chivalry\" } ] } "));
}
@Test // DATAMONGO-2052
public void toArrayWithFieldReference() {
assertThat(ObjectOperators.valueOf("verity").toArray().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(Document.parse("{ $objectToArray : \"$verity\" }"));
}
@Test // DATAMONGO-2052
public void toArrayWithExpression() {
assertThat(ObjectOperators.valueOf(EXPRESSION).toArray().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(Document.parse("{ $objectToArray : " + EXPRESSION_STRING + " }"));
}
}

View File

@@ -297,6 +297,7 @@ error occurs here.
<3> An error outside the transaction flow has no affect on the previous transactional execution.
====
[[mongo.transactions.behavior]]
== Special behavior inside transactions
Inside transactions, MongoDB server has a slightly different behavior.
@@ -321,6 +322,14 @@ The server responds with _error 50851_ when issuing a `count` command inside of
Once `MongoTemplate` detects an active transaction, all exposed `count()` methods are converted and delegated to the
aggregation framework using `$match` and `$count` operators, preserving `Query` settings, such as `collation`.
Restrictions apply when using geo commands inside of the aggregation count helper. The following operators cannot be used and must be replaced with a different operator:
* `$where` -> `$expr`
* `$near` -> `$geoWithin` with `$center`
* `$nearSphere` -> `$geoWithin` with `$centerSphere`
Queries using `Criteria.near(…)` and `Criteria.nearSphere(…)` must be rewritten to `Criteria.within(…)` respective `Criteria.withinSphere(…)`. Same applies for the `near` query keyword in repository query methods that must be changed to `within`. See also MongoDB JIRA ticket https://jira.mongodb.org/browse/DRIVERS-518[DRIVERS-518] for further reference.
The following snippet shows `count` usage inside the session-bound closure:
====

View File

@@ -1201,6 +1201,8 @@ template.query(Person.class) <1>
MongoDB supports GeoSpatial queries through the use of operators such as `$near`, `$within`, `geoWithin`, and `$nearSphere`. Methods specific to geospatial queries are available on the `Criteria` class. There are also a few shape classes (`Box`, `Circle`, and `Point`) that are used in conjunction with geospatial related `Criteria` methods.
NOTE: Using GeoSpatial queries requires attention when used within MongoDB transactions, see <<mongo.transactions.behavior>>.
To understand how to perform GeoSpatial queries, consider the following `Venue` class (taken from the integration tests and relying on the rich `MappingMongoConverter`):
[source,java]
@@ -2126,7 +2128,7 @@ At the time of this writing, we provide support for the following Aggregation Op
| `eq` (*via: `is`), `gt`, `gte`, `lt`, `lte`, `ne`
| Array Aggregation Operators
| `arrayElementAt`, `concatArrays`, `filter`, `in`, `indexOfArray`, `isArray`, `range`, `reverseArray`, `reduce`, `size`, `slice`, `zip`
| `arrayElementAt`, `arrayToObject`, `concatArrays`, `filter`, `in`, `indexOfArray`, `isArray`, `range`, `reverseArray`, `reduce`, `size`, `slice`, `zip`
| Literal Operators
| `literal`
@@ -2147,7 +2149,7 @@ At the time of this writing, we provide support for the following Aggregation Op
| `convert`, `toBool`, `toDate`, `toDecimal`, `toDouble`, `toInt`, `toLong`, `toObjectId`, `toString`
| Object Aggregation Operators
| `mergeObjects`
| `objectToArray`, `mergeObjects`
|===
* The operation is mapped or added by Spring Data MongoDB.
@@ -3083,4 +3085,4 @@ class GridFsClient {
`GridFsOperations` extends `ResourcePatternResolver` and lets the `GridFsTemplate` (for example) to be plugged into an `ApplicationContext` to read Spring Config files from MongoDB database.
include::change-streams.adoc[]
include::change-streams.adoc[]

View File

@@ -1,6 +1,25 @@
Spring Data MongoDB Changelog
=============================
Changes in version 2.1.0.RC2 (2018-08-20)
-----------------------------------------
* DATAMONGO-2055 - Allow position modifier to be negative using push at position on Update.
* DATAMONGO-2053 - Add support for $mergeObjects aggregation operator.
* DATAMONGO-2052 - Add support for MongoDB 3.6 array aggregation operators.
* DATAMONGO-2051 - Add support for SCRAM-SHA-256 authentication mechanism to MongoCredentialPropertyEditor.
* DATAMONGO-2050 - Add support for index selection via key attribute for $geoNear aggregation.
* DATAMONGO-2049 - Add support for MongoDB 4.0 string aggregation operators.
* DATAMONGO-2048 - Add support for MongoDB 4.0 type conversion aggregation operators.
* DATAMONGO-2047 - Update $dateToString and $dateFromString aggregation operators to match MongoDB 4.0 changes.
* DATAMONGO-2046 - Investigate performance regressions between 2.0 GA and 2.1 RC2.
* DATAMONGO-2045 - Add transaction specific error codes for exception translation.
* DATAMONGO-2043 - MappingMongoConverter.write(…) does not consider Document a native type and adds _class attribute.
* DATAMONGO-2041 - Automatically derive the fields to be read from the expected result type.
* DATAMONGO-2040 - Update mongodb docs to reflect Duplicates.DROP is no longer supported.
* DATAMONGO-2033 - Release 2.1 RC2 (Lovelace).
* DATAMONGO-2027 - outputCollection and outputType of MapReduceOptions not work.
Changes in version 1.10.14.RELEASE (2018-07-27)
-----------------------------------------------
* DATAMONGO-2023 - ClassCastException for $sample aggregation operation.

View File

@@ -1,4 +1,4 @@
Spring Data MongoDB 2.1 RC1
Spring Data MongoDB 2.1 RC2
Copyright (c) [2010-2015] Pivotal Software, Inc.
This product is licensed to you under the Apache License, Version 2.0 (the "License").