Skip to content
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

Implement $set stage dsl #104

Merged
merged 6 commits into from
Oct 29, 2023
Merged

Implement $set stage dsl #104

merged 6 commits into from
Oct 29, 2023

Conversation

username1103
Copy link
Contributor

resolve #72

Comment on lines 118 to 131
/**
* @see <a href="https://www.mongodb.com/docs/manual/reference/operator/aggregation/set/#add-element-to-an-array">Add Element to an Array</a>
*/
fun addElementToAnArray(): AggregationResults<Score> {
val aggregation = aggregation {
match(Criteria.where(Score::id.toDotPath()).isEqualTo(1))
set {
// TODO: add $concatArrays expression
Score::homework set Document("\$concatArrays", listOf("\$${Score::homework.toDotPath()}", listOf(7)))
}
}

return mongoTemplate.aggregate<Score>(aggregation, SCORES)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In example, we have to add 7 at the end of "homework" array.

db.scores.aggregate([
   { $match: { _id: 1 } },
   { $set: { homework: { $concatArrays: [ "$homework", [ 7 ] ] } } }
])

But, ConcatArrays not supported this. We can only concat value at the beginning maybe..

https://github.com/spring-projects/spring-data-mongodb/blob/69285151a0a8fc30b90cda06505bef973ea7151b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ArrayOperatorsUnitTests.java#L77-L82

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about using literal expression. literals(listof(7))
but I can not sure if it works

Copy link
Contributor Author

@username1103 username1103 Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 it work well.

Copy link
Contributor

@jbl428 jbl428 Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think it is better to implement $concatArrays without using ArrayOperator from spring.
It seems fine to leave it as it for now :)

Comment on lines +27 to +29
set {
SalesDto::id set "2017"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍 👍

Copy link
Contributor

@jbl428 jbl428 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jbl428 jbl428 merged commit d1ee223 into inflearn:main Oct 29, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement $set stage dsl
2 participants