'Using MongoDB $arrayToObject and $map operators in java spring boot

I have a query in mongodb with aggregation framework, which returns number of topics in my documents. This is done by utilizing the $group operator and the result looks like:

{
postsByTopics: [
        {
            "_id" : "Topic 1",
            "count" : 3.0
        },
        {
            "_id" : "Topic 2",
            "count" : 1.0
        }
    ]
}

I use projection to get a better shaped results

{
"$project": {
        "postsByTopics": {
            "$arrayToObject": {
                "$map": {
                    "input": "$postsByTopics",
                    "as": "el",
                    "in": {
                        "k": "$$el._id",
                        "v": "$$el.count",
                    }
                }
            }
        },

}

which returns much nicer shaped result:

{
postsByTopics: {
    "Topic1" : 3.0,
    "Topic2" : 1.0
    }
}

Now I would like to turn this projection in a java spring boot code. I found project().and(ArrayOperators.ArrayToObject.arrayToObject("postsByTopics"))

and

project().and(VariableOperators.Map.itemsOf("postsByTopics").as("el")

but I am struggling to combine them to achieve the desired result



Solution 1:[1]

Code above is NoSQL Query and down is almost similiar with MongoTemplate Cirteria Query. Maybe same propertieies are different, hope it will help someone ! Important is to save List as projection new property, i didnt find solution with arrayOf(List ... always inserted List in list with input: { elemetnAt: [[ .... whci hwas really wrong.

   $project {
     stats: {
         $map: {
             input: [ "2018-09-01", "2022-01-24", "2022-01-22", "2018-09-04", "2018-09-05" ],
             as: "date",
             in: {
             $let: {
                 vars: { dateIndex: { "$indexOfArray": [ "$days.day", "$$date" ] } },
                 in: {
                     $cond: {
                         if: { $ne: [ "$$dateIndex", -1 ] },
                         then: { $arrayElemAt: [ "$days", "$$dateIndex" ] },
                         else: { _id: "$$date", date: "$$date", distance: 0 }
                     }
                 }
                }
             }
         }
     }





project("_id", "days", "category").andArrayOf(timearray).as("daysEmpty"),
                project("category").and(
                        mapItemsOf("daysEmpty")
                                .as("date")
                                .andApply(new AggregationExpression() {
                                    @Override
                                    public Document toDocument(AggregationOperationContext context) {
                                       return new Document("$let",
                                                new Document("vars",
                                                        new Document("dateIndex",
                                                                new Document("$indexOfArray", asList("$days.day", "$$date"))))
                                                        .append("in",
                                                                new Document("$cond",
                                                                        new Document("if",
                                                                                new Document("$ne", asList("$$dateIndex", -1L)))
                                                                                .append("then",
                                                                                        new Document("$arrayElemAt", asList("$days", "$$dateIndex")))
                                                                                .append("else",
                                                                                        new Document("_id", "$$date")
                                                                                                .append("date", "$$date")
                                                                                                .append("distance", 0L)
                                                                                )
                                                                )
                                                        )
                                        );
                                    }
                                })).as("data")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kopeclu2