Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
MongoTemplate aggregation replaceRoot not working

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!