spring data - Mongodb - findBy Method for nested objects

前端 未结 3 1237
一个人的身影
一个人的身影 2020-12-01 06:41

I have two domain objects,

@Document
public class PracticeQuestion {

     private int userId;
     private List questions;

// Getters and s         


        
相关标签:
3条回答
  • 2020-12-01 06:59

    Property expressions

    Property expressions can refer only to a direct property of the managed entity, as shown in the preceding example. At query creation time you already make sure that the parsed property is a property of the managed domain class. However, you can also define constraints by traversing nested properties. Assume Persons have Addresses with ZipCodes. In that case a method name of List<Person> findByAddressZipCode(ZipCode zipCode); creates the property traversal x.address.zipCode. The resolution algorithm starts with interpreting the entire part (AddressZipCode) as the property and checks the domain class for a property with that name (uncapitalized). If the algorithm succeeds it uses that property. If not, the algorithm splits up the source at the camel case parts from the right side into a head and a tail and tries to find the corresponding property, in our example, AddressZip and Code. If the algorithm finds a property with that head it takes the tail and continue building the tree down from there, splitting the tail up in the way just described. If the first split does not match, the algorithm move the split point to the left (Address, ZipCode) and continues.

    Although this should work for most cases, it is possible for the algorithm to select the wrong property. Suppose the Person class has an addressZip property as well. The algorithm would match in the first split round already and essentially choose the wrong property and finally fail (as the type of addressZip probably has no code property). To resolve this ambiguity you can use _ inside your method name to manually define traversal points. So our method name would end up like so:

    UserDataRepository:

    List<UserData> findByAddress_ZipCode(ZipCode zipCode);
    
    UserData findByUserId(String userId);
    

    ProfileRepository:

    Profile findByProfileId(String profileId);
    

    UserDataRepositoryImpl:

    UserData userData =  userDateRepository.findByUserId(userId);
    
    Profile profile = profileRepository.findByProfileId(userData.getProfileId());
    
    userData.setProfile(profile);
    

    Sample Pojo :

    public class UserData {
    
        private String userId;
        private String status;
        private Address address;
        private String profileId;
    
        //New Property
        private Profile profile;
    
        //TODO:setter & getter
    }
    
    public class Profile {
    
        private String email;
        private String profileId;
    }
    

    For the above Document/POJO in your Repository Class:

    UserData findByProfile_Email(String email);

    For ref : http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

    0 讨论(0)
  • 2020-12-01 07:13

    You need to use Mongo Aggregation framework :

    1) Create custom method for mongo repository : Add custom method to Repository

    UnwindOperation unwind =  Aggregation.unwind("questions");
    MatchOperation match = Aggregation.match(Criteria.where("userId").is(userId).and("questions.questionId").is(questionID));
    Aggregation aggregation = Aggregation.newAggregation(unwind,match);
    AggregationResults<PracticeQuestionUnwind> results = mongoOperations.aggregate(aggregation, "PracticeQuestion",
                    PracticeQuestionUnwind.class);
    return results.getMappedResults();
    

    2) You need to cretae a class(Because unwind operation has changed the class structure) like below :

    public class PracticeQuestionUnwind {
        private String userId;
        private Question questions;
    

    This will give you only those result which matches the provide userId and questionId

    Result for userId: 1 and questionId : 111 :

    {
        "userId": "1",
         "questions": {
                    "questionId": "111",
                    "type": "optional"
                 }
     }
    
    0 讨论(0)
  • 2020-12-01 07:15

    Just use the @Query annotation on that method.

    public interface CustomRepository extends MongoRepository<PracticeQuestion, String> {
    
        @Query(value = "{ 'userId' : ?0, 'questions.questionID' : ?1 }", fields = "{ 'questions.questionID' : 1 }")
        List<PracticeQuestion> findByUserIdAndQuestionsQuestionID(int userId, int questionID);
    
    }
    

    By adding the fields part of the @Query annotation, you are telling Mongo to only return that part of the document. Beware though, it still returns the entire document in the same format - just missing everything you did not specify. So your code will still have to return List<PracticeQuestion> and you will have to do:

    foreach (PracticeQuestion pq : practiceQuestions) {
        Question q = pq.getQuestions().get(0); // This should be your question.
    }
    
    0 讨论(0)
提交回复
热议问题