Skip to content

DATAMONGO-3694 - Add support for $first & $last aggregation operator #3866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4491301
Polishing.
mp911de Aug 26, 2021
2d8d66d
taking latest from main.
divyajnu08 Aug 29, 2021
037cac0
resolving merge conflict
divyajnu08 Aug 29, 2021
1010f81
Merge branch 'spring-projects-main'
divyajnu08 Aug 29, 2021
4cd6cf9
Merge branch 'spring-projects:main' into main
divyajnu08 Sep 10, 2021
3430298
Merge branch 'spring-projects:main' into main
divyajnu08 Sep 10, 2021
703b2eb
Merge branch 'spring-projects:main' into main
divyajnu08 Sep 14, 2021
825eff7
Merge branch 'spring-projects:main' into main
divyajnu08 Sep 14, 2021
853e10a
Merge branch 'spring-projects:main' into main
divyajnu08 Sep 18, 2021
e3c4202
Merge branch 'spring-projects:main' into main
divyajnu08 Sep 21, 2021
53fcdb7
Merge branch 'spring-projects:main' into main
divyajnu08 Sep 27, 2021
eb997b5
Merge branch 'spring-projects:main' into main
divyajnu08 Oct 5, 2021
0917e68
DATAMONGO-2637 - Fields and projection API are very inconvenient
divyajnu08 Sep 27, 2021
e830626
Revert "DATAMONGO-2637 - Fields and projection API are very inconveni…
divyajnu08 Sep 28, 2021
e3bdf33
DATAMONGO-2637 - Fields and projection API are very inconvenient
divyajnu08 Sep 27, 2021
857e065
Revert "DATAMONGO-2637 - Fields and projection API are very inconveni…
divyajnu08 Sep 28, 2021
e003f04
Merge branch 'spring-projects:main' into main
divyajnu08 Oct 9, 2021
a499d85
Merge branch 'main' of https://github.com/divyajnu08/spring-data-mongodb
divyajnu08 Oct 9, 2021
07d66af
Merge branch 'spring-projects:main' into main
divyajnu08 Oct 16, 2021
84f521e
Merge branch 'main' of https://github.com/divyajnu08/spring-data-mongodb
divyajnu08 Oct 16, 2021
fc9bce7
DATAMONGO-3694 - Add support for $first & $last aggregation operator
divyajnu08 Oct 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,36 @@ public ArrayToObject toObject() {

return usesExpression() ? ArrayToObject.arrayValueOfToObject(expression) : ArrayToObject.arrayToObject(values);
}

/**
* Creates new {@link AggregationExpression} that return the first element in the given array.
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
*
* @return new instance of {@link First}.
*/
public First first() {

if (usesFieldRef()) {
return First.firstOf(fieldReference);
}

return usesExpression() ? First.firstOf(expression) : First.first(values);
}

/**
* Creates new {@link AggregationExpression} that return the last element in the given array.
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
*
* @return new instance of {@link First}.
*/
public Last last() {

if (usesFieldRef()) {
return Last.lastOf(fieldReference);
}

return usesExpression() ? Last.lastOf(expression) : Last.last(values);
}

/**
* @author Christoph Strobl
Expand Down Expand Up @@ -1812,4 +1842,107 @@ protected String getMongoMethod() {
return "$arrayToObject";
}
}

/**
* {@link AggregationExpression} for {@code $first} that returns the first element in an array. <br />
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
*
*/
public static class First extends AbstractAggregationExpression {

private First(Object value) {
super(value);
}

/**
* Returns the first element in the given array.
*
* @param array must not be {@literal null}.
* @return new instance of {@link First}.
*/
public static First first(Object array) {
return new First(array);
}

/**
* Returns the first element in the array pointed to by the given {@link Field field reference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link First}.
*/
public static First firstOf(String fieldReference) {
return new First(Fields.field(fieldReference));
}

/**
* Returns the first element of the array of the given {@link AggregationExpression expression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link First}.
*/
public static First firstOf(AggregationExpression expression) {
return new First(expression);
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
*/
@Override
protected String getMongoMethod() {
return "$first";
}
}

/**
* {@link AggregationExpression} for {@code $last} that returns the last element in an array. <br />
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
*
*/
public static class Last extends AbstractAggregationExpression {

private Last(Object value) {
super(value);
}

/**
* Returns the first element in the given array.
*
* @param array must not be {@literal null}.
* @return new instance of {@link Last}.
*/
public static Last last(Object array) {
return new Last(array);
}

/**
* Returns the last element in the array pointed to by the given {@link Field field reference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link Last}.
*/
public static Last lastOf(String fieldReference) {
return new Last(Fields.field(fieldReference));
}

/**
* Returns the last element of the array of the given {@link AggregationExpression expression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Last}.
*/
public static Last lastOf(AggregationExpression expression) {
return new Last(expression);
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
*/
@Override
protected String getMongoMethod() {
return "$last";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,47 @@ public void inWithValueList() {
assertThat(ArrayOperators.arrayOf(VALUE_LIST).containsValue("$userName").toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ \"$in\" : [\"$userName\", " + VALUE_LIST_STRING + "] }");
}

@Test
public void firstWithValueList() {

assertThat(ArrayOperators.arrayOf(VALUE_LIST).first().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ \"$first\" : " + VALUE_LIST_STRING + "}");
}

@Test
public void firstWithExpression() {

assertThat(ArrayOperators.arrayOf(EXPRESSION).first().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ \"$first\" : " + EXPRESSION_STRING + "}");
}

@Test
public void firstWithFieldReference() {

assertThat(ArrayOperators.arrayOf("field").first().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $first : \"$field\" }");
}

@Test
public void lastWithValueList() {

assertThat(ArrayOperators.arrayOf(VALUE_LIST).last().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ \"$last\" : " + VALUE_LIST_STRING + "}");
}

@Test
public void lastWithExpression() {

assertThat(ArrayOperators.arrayOf(EXPRESSION).last().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ \"$last\" : " + EXPRESSION_STRING + "}");
}

@Test
public void lastWithFieldReference() {

assertThat(ArrayOperators.arrayOf("field").last().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $last : \"$field\" }");
}

}