I'm using Spring Data MongoDB to generate an aggregated query. At one point I do this:
// 5. Rejoin the array with group.
group("email", "name", "surname", "birthday", "creationTime", "updateTime", "technology")
.push(SCORES_FIELD).as(SCORES_FIELD));
The generated step (in the log) is this:
"$group" : {
"_id" : {
"email" : "$_id",
"name" : "$name" ,
"surname" : "$surname" ,
"birthday" : "$birthday" ,
"creationTime" : "$creationTime" ,
"updateTime" : "$updateTime" ,
"technology" : "$technology"
} ,
"scores" : { "$push" : "$scores"}
}
Which is perfectly fine, I've tested it on the Mongo shell and gives back exactly what I want.
The problem is that when I do the same with Spring Data, the email field (which is the _id field in Mongo) is mapped as null. There's probably something wrong in my mapping but I haven't been able to figure out what is it exactly. Here's the model:
@Document(collection = "user")
public class User implements UserDetails {
private static final long serialVersionUID = 1L;
private String name;
private String surname;
private LocalDate birthday;
@Id
@Field("_id")
private String email;
private Collection<? extends GrantedAuthority> authorities;
private String password;
private Set<Score> scores;
private LocalDateTime creationTime;
private LocalDateTime updateTime;
private String technology;
// Getters and setters, hashmap, equals and toString
}
I've done other queries and everything works out perfectly. I'm having this problem only on this one which is the only aggregation I do.
group(fields( "name", "surname", "birthday", "creationTime", "updateTime", "technology").and("email", previousOperation())).push(SCORES_FIELD).as(SCORES_FIELD));- s7vrpreviousOperation()is just a convenience method to return the_idfrom previous group operation. You can try changing togroup("email").first("name").as("name")....and see if it helps. I would expect spring to read theFieldannotation from the model and map the _id field back to email now. - s7vr