1

I wrote a shell as below which works fine in mongo console, I am wondering how to write the Java code in spring mongo. I tried many approches with no success, any help will be grateful.

db.getCollection('Application').aggregate([

  {$match: {"applicationId":"2021010706001"}},
  {$group: { _id: "$applicationId", mapData: { $mergeObjects: "$mapData" } } },
  {$replaceRoot: { newRoot: "$mapData" } }
])
1

1 Answer 1

1

finally I have worked it out, as below:

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("applicationId").is(applicationId)),
                Aggregation.group("applicationId").addToSet("mapData").as("mapData"),
                Aggregation.project("mapData"),
                Aggregation.replaceRoot().withValueOf(
                        ObjectOperators.valueOf("mapData").merge())
        );
1
  • addToSet is not the best replace of $mergeObjects because it convert documents into array, which does not hold order of documents if you made sort in previous stage of aggregation Sep 6, 2022 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.