代码之家  ›  专栏  ›  技术社区  ›  Aurasphere

Spring data MongoDB无法在组聚合中映射\u id

  •  2
  • Aurasphere  · 技术社区  · 8 年前

    我正在使用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
    
    }
    

    我做了其他的查询,一切都很顺利。我只有这个问题,这是我唯一做的聚合。

    1 回复  |  直到 8 年前
        1
  •  2
  •   s7vr    8 年前

    推广我的评论以回答。

    无法将id映射到电子邮件中,因为group stage在id文档中返回多个密钥。 previousOperation() 只是一种从以前的组操作返回\u id的方便方法。您可以尝试更改为 group("email").first("name").as("name").... 看看是否有帮助。

    我希望spring现在能够从模型中读取字段注释并将id字段映射回电子邮件。