简体   繁体   English

如何在Spring Data MongoDB存储库的查询方法中使用投影类型?

[英]How to use projecting types in Spring Data MongoDB repository's query methods?

I have been playing around with Spring Data and MongoDB and have a question about limiting the amount of data for certain queries. 我一直在使用Spring Data和MongoDB,并且有关于限制某些查询的数据量的问题。 I've seen adding custom queries within the MongoRepository , however I haven't seen any examples of limiting the amount of data and returning classes that are basically a subset of larger classes. 我已经看到在MongoRepository添加自定义查询,但是我没有看到任何限制数据量和返回基本上是较大类的子集的类的示例。

For instance I have a User class that has several fields, but I also want to create a UserShort class that has a subset of the fields in the User class. 例如,我有一个包含多个字段的User类,但我还想创建一个UserShort类,该类包含User类中的字段子集。 For instance UserShort would only contain the id and firstName / lastName / email fields, rather than everything. 例如, UserShort只包含idfirstName / lastName / email字段,而不是所有内容。

I've seen I can specify/limit the fields that are returned, but can I have those returned into a different class? 我已经看到我可以指定/限制返回的字段,但是我可以让那些返回到不同的类吗? At the moment the UserShort will return null unless I specify the User class instead, but the fields will be limited to the ones I specify. 除非我指定User类,否则UserShort将返回null ,但字段将限制为我指定的字段。 Wasn't sure if this is possible at all? 不确定这是否可行? I realize the User class below isn't huge, but it's the concept I'm after. 我意识到下面的User类并不大,但它是我追求的概念。

A user interface: 用户界面:

public interface Users {}

Subset class: 子集类:

public class UserShort implements Users {

    @Id
    private String id;

    private String firstName;
    private String lastName;

    @Indexed(unique = true)
    private String email;

    //getters / setters
}

Full class: 全班:

@Document
public class User implements Users {

    @Id
    private String id;

    private String firstName;
    private String lastName;
    private String username;
    private String password;
    private Date dob;
    private Status status;

    @Indexed(unique = true)
    private String email;

    private Gender gender;
    private String locale;
    private Date registerDate;

    @DBRef
    private List<UserAddress> addresses;

    public User(){
        addresses = new ArrayList<UserAddress>();
    }

    //getters / setters
}

And the repository interface: 和存储库接口:

public interface UserRepository extends MongoRepository<Users, String> {

    public User findByEmail(String email);

    @Query(value="{ 'email' : ?0 }", fields="{ 'firstName' : 1, 'lastName' : 1}")
    public UserShort findUserShortByEmail(String email);

}

As long as the return type of the query method is assignable to the managed domain type ( Users in your case) we will prefer the return type to determine the collection to run the query against. 只要查询方法的返回类型可分配给托管域类型(在您的情况下为Users ),我们将更喜欢返回类型来确定要运行查询的集合。 Thus, in your case we'd execute the query against userShort instead of users which is why you do not get any results. 因此,在您的情况下,我们将针对userShort而不是users执行查询,这就是您没有获得任何结果的原因。 That behavior is in place to support storing inheritance hierarchies into different collections. 该行为适用于支持将继承层次结构存储到不同的集合中。

If you switched to User as the domain type for the repository, things should work exactly as expected. 如果您切换到User作为存储库的域类型,事情应该完全按预期工作。 This would also have the benefit of preventing clients from handing UserShort instances to the save(…) method which will wipe out properties contained in User but not in UserShort . 这还有一个好处,就是可以防止客户端将UserShort实例交给save(…)方法,该方法将清除User包含但不包含在UserShort中的UserShort Here's the final repository interface declaration. 这是最终的存储库接口声明。

interface UserRepository extends MongoRepository<User, String> {

    User findByEmail(String email);

    @Query(value="{ 'email' : ?0 }", fields="{ 'firstName' : 1, 'lastName' : 1}")
    UserShort findUserShortByEmail(String email);
}

PS: @byte-crunch outlined in the comments that this currently only works for collections of projections but not for returning single instances. PS:在评论中概述了@ byte-crunch,它目前仅适用于投影集合,但不适用于返回单个实例。 This has been reported and fixed in DATAMONGO-1030 and will be available in 1.5.4 and 1.6.0 GA. 这已在DATAMONGO-1030中报告并修复, 并将在1.5.4和1.6.0 GA中提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM