Home > Software design >  MongoTemplate aggregation replaceRoot not working
MongoTemplate aggregation replaceRoot not working

Time:02-26

I'm working with SpringBoot Mongodb using Mongotemplate.

I have a packs collection with following sample document:

{
    "_id" : ObjectId("61de8228a992b10804b3f1ae"),
    "pack_uuid" : "f67cb514-326c-4933-8e23-0580c912896c",
    "pack_name" : "covid 19",
    "pack_description" : "covid is dengeours",
    "created_by" : "ea41c6b8-aaec-4d9b-a5a7-a6a72ca4c9f6",
    "members" : [{
        "_id" : "0aa4b098-aab3-4ccf-8401-d52e6a4e42cc",
        "user_uuid" : "1f9b5f0b-f9f6-45d7-b25b-a5319307569f",
        "joining_date" : ISODate("2022-01-12T16:24:34.719Z")
    },{
        "_id" : "0aa4b098-aab3-4ccf-8401-d52e6a4e42cc",
        "user_uuid" : "2f9b5f0b-f9f6-45d7-b25b-a5319307569f",
        "joining_date" : ISODate("2022-01-12T16:24:34.719Z")
    }]
}

I need to implement where condition that checks if passed user is a member of pack or not. Something like:

"members.user_uuid": "1f9b5f0b-f9f6-45d7-b25b-a5319307569f"

and return only following fields:

"pack_uuid" : "f67cb514-326c-4933-8e23-0580c912896c",
"pack_name" : "covid 19",
"pack_description" : "covid is dengeours",
"created_by" : "ea41c6b8-aaec-4d9b-a5a7-a6a72ca4c9f6",

I tried following code:

AggregationOperation match = Aggregation.match(
    Criteria.where("members.user_uuid").is(userUUID)
);
AggregationOperation unwind = Aggregation.unwind("members");
AggregationOperation group = Aggregation.group("_id");
AggregationOperation replaceRoot = Aggregation.replaceRoot(Aggregation.ROOT);

List<AggregationOperation> operations = new ArrayList<>();
operations.add(unwind);
operations.add(match);
operations.add(group);
operations.add(replaceRoot);

Aggregation aggregation = Aggregation.newAggregation(operations);

List<Pack> packs = mongoTemplate.aggregate(aggregation, Pack.class, Pack.class).getMappedResults();

I'm getting this error:

Invalid reference '$$ROOT'

What am I doing wrong?

CodePudding user response:

You don't need replaceRoot in this case since the fields you want to project are already at root. Also, no need for the $unwind and $group pipelines as $match is sufficient enough to return matching documents for the given query.

Instead, exclude the members field by using ProjectionOperation as follows:

AggregationOperation match = Aggregation.match(
    Criteria.where("members.user_uuid").is(userUUID)
);
ProjectionOperation project = Aggregation.project().andExclude("members");

List<AggregationOperation> operations = new ArrayList<>();
operations.add(match);
operations.add(project);

Aggregation aggregation = Aggregation.newAggregation(operations);

List<Pack> packs = mongoTemplate.aggregate(aggregation, Pack.class, Pack.class).getMappedResults();
  • Related