-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support expressions in query field projections. #3585
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
Conversation
* @param <T> | ||
* @return never {@literal null}. | ||
*/ | ||
default <T extends MongoExpression> T as(Function<MongoExpression, T> function) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about map
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentionally did not name this one map
, as I wanted to keep that method name available. I sounds like a better fit for getting called by the mapping layer to apply conversion.
* @see org.springframework.data.mongodb.core.aggregation.DateOperators | ||
* @see org.springframework.data.mongodb.core.aggregation.ObjectOperators | ||
* @see org.springframework.data.mongodb.core.aggregation.SetOperators | ||
* @see org.springframework.data.mongodb.core.aggregation.StringOperators @ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: There's a lingering @
* @param json must not be {@literal null}. | ||
* @return new instance of {@link MongoExpression}. | ||
*/ | ||
static MongoExpression expressionFromString(String json) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about parse
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things:
- static import clashes with eg.
org.bson.Document.parse
- the expression must not be evaluated immediately which is something I'd expect from
parse
.
// explicit via dedicated AggregationExpression query.fields() .project(StringOperators.valueOf("name").toUpper()) .as("name"); // with a user provided expression parsed from a String query.fields().project(MongoExpression.from("'$toUpper' : '$name'")) .as("name") // using SpEL support query.fields().project(AggregationSpELExpression.expressionOf("toUpper(name)")) .as("name"); // with parameter binding query.fields().project( MongoExpression.from("'$toUpper' : '?0'").bind("$name") ).as("name") // via the @query annotation on repositories @query(value = "{ 'id' : ?0 }", fields = "{ 'name': { '$toUpper': '$name' } }")
- MongoExpresssion#expressionFromString(String) -> MongoExpresssion#create - AggregationExpression#create(MongoExpression) ->A ggregationExpression#from - MongoExpression#as -> removed from public API
a2b25fb
to
ff9d5a9
Compare
// explicit via dedicated AggregationExpression query.fields() .project(StringOperators.valueOf("name").toUpper()) .as("name"); // with a user provided expression parsed from a String query.fields().project(MongoExpression.create("'$toUpper' : '$name'")) .as("name") // using SpEL support query.fields().project(AggregationSpELExpression.expressionOf("toUpper(name)")) .as("name"); // with parameter binding query.fields().project( MongoExpression.create("'$toUpper' : '?0'").bind("$name") ).as("name") // via the @query annotation on repositories @query(value = "{ 'id' : ?0 }", fields = "{ 'name': { '$toUpper': '$name' } }") Closes: #3583 Original pull request: #3585.
Starting with MongoDB 4.4 it is possible to use the aggregation expressions syntax for field projections as shown below.
@Query(fields='...')
allows usage of expression field projections atRepository
level.Closes: #3583