我正在使用Spring数据MongoDB生成聚合查询。我曾经这样做过:
// 5. Rejoin the array with group.
group("email", "name", "surname", "birthday", "creationTime", "updateTime", "technology")
.push(SCORES_FIELD).as(SCORES_FIELD));
生成的步骤(在日志中)如下:
"$group" : {
"_id" : {
"email" : "$_id",
"name" : "$name" ,
"surname" : "$surname" ,
"birthday" : "$birthday" ,
"creationTime" : "$creationTime" ,
"updateTime" : "$updateTime" ,
"technology" : "$technology"
} ,
"scores" : { "$push" : "$scores"}
}
这很好,我已经在Mongo shell上进行了测试,并给出了我想要的。
问题是,当我对Spring数据执行相同操作时,email字段(Mongo中的\u id字段)被映射为null。我的映射可能有问题,但我还没有弄清楚到底是什么。模型如下:
@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
}
我做了其他的查询,一切都很顺利。我只有这个问题,这是我唯一做的聚合。