MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Map and Reduce Operation in Mongo-Db Query using JAVA - Springboot

I can not able to do map and reduce operation in java mongodb in ProjectionOperation.

I have project operation in native mongo query like this:

{
    $project: {
        data: {
            $map: {
                input: params.timeArray,
                in: {
                    "key": "$this",
                    "value": { "$cond": [{ "$in": ["$this", "$data.date"] }, "$data", []] }
                }
            }
        }
    }
}

And I also want to do reduce operation in another projection operation like this:

{
    $project: {
        id: 1,
        data: { $reduce: { input: "$data", initialValue: [], in: { $concatArrays: ["$value", "$this"] } } }
    }
}

I have tried this:

ProjectionOperation projectionOperation1 = Aggregation.project()
                .and(VariableOperators.mapItemsOf("data")
                        .as("input")
                        .andApply(context -> new BasicDBObject("value",
                                ConditionalOperators.when(where("$this").in("$data.date"))
                                        .then("$data")
                                        .otherwise(new HashSet<>()))))
                .as("data");

But it didn’t give me desired result.

Any help would be appreciated!!

Would you explain what your original document looks like, and what result you are trying to get?

That can be helpful to understand what your code is trying to do.

Did you mean to use "$$this" to reference the variable?

I just noticed in $reduce you’re also using $this and $value instead of $$this and $$value.

Yesss[quote=“Prem_Parmar, post:1, topic:1856, full:true”]
I can not able to do map and reduce operation in java mongodb in ProjectionOperation.

I have project operation in native mongo query like this:

{
    $project: {
        data: {
            $map: {
                input: params.timeArray,
                in: {
                    "key": "$this",
                    "value": { "$cond": [{ "$in": ["$this", "$data.date"] }, "$data", []] }
                }
            }
        }
    }
}

And I also want to do reduce operation in another projection operation like this:

{
    $project: {
        id: 1,
        data: { $reduce: { input: "$data", initialValue: [], in: { $concatArrays: ["$value", "$this"] } } }
    }
}

I have tried this:

ProjectionOperation projectionOperation1 = Aggregation.project()
                .and(VariableOperators.mapItemsOf("data")
                        .as("input")
                        .andApply(context -> new BasicDBObject("value",
                                ConditionalOperators.when(where("$this").in("$data.date"))
                                        .then("$data")
                                        .otherwise(new HashSet<>()))))
                .as("data");

But it didn’t give me desired result.

Any help would be appreciated!!
[/quote]

Yes, “$$this” is reference of the variable

Can you check that you’re using TWO dollar signs as prefix? Your examples show one dollar sign.

  • "$this" says "value of field named 'this'"

  • "$$this" says "value of variable named 'this'"