Aggregation and sorting based on subdocument property

Hi,
I have 1 parent document and 2 nested documents

Document
public class F {

    private String id;

DBRef
    private S s;

DBRef
    private P p;

private String status;
}

Document
public class S {
    private String id;
}

Document
public class P {
    private String id;

private long r;
}

now I have in hand subdocument S id and document F status .I want to fetch 5 F documents, based on subdocument S id (“123334”) , Document F status (“COMPLETE”) and the result should be sorted by subdocument P, r property ?

I have tried the below spring-data, but the sorting never worked

Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("status").is("COMPLETE")),
                Aggregation.unwind("s"),
                Aggregation.match(Criteria.where("s.$id").is(new ObjectId("123334"))),
                Aggregation.unwind("p"),
                Aggregation.sort(new Sort(Sort.Direction.DESC, "p.r")),
                Aggregation.limit(5));
        List<F> fs = mongoOperation.aggregate(aggregation, "f", F.class).getMappedResults();

This question does not seem to be related to the M121 course.

You will have better luck with having a appropriate answer by posting to the specific driver thread you are using. If your question is not specific to your driver then it is best to publish real sample documents from your collections. It is really hard and time consuming to generate our own sample documents from your class definitions.

I switched to MongoDb for Java Devs, hope this is the correct forum