MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Come filtrare l'array nel documento Mongodb usando Spring

Come hai detto, elemMatch recupererà solo il primo oggetto in un array, quindi devi utilizzare il futuro aggregato per ottenere il tuo output

    MatchOperation match = Aggregation.match(Criteria.where("_id.teacherId").is("1").and("groups.students").in(11));
    UnwindOperation unwind = Aggregation.unwind("groups");
    GroupOperation group = Aggregation.group("_id").push("groups").as("groups").first("points").as("points");
    Aggregation aggregation = Aggregation.newAggregation(unwind, match, group);
    AggregationResults<BasicDBObject> groupResults = mongoTemplate.aggregate(aggregation,
                    CustomGroupsDocument.class, BasicDBObject.class);
    List<BasicDBObject> result = groupResults.getMappedResults();