spring data - Mongodb - findBy Method for nested objects












17














I have two domain objects,



@Document
public class PracticeQuestion {

private int userId;
private List<Question> questions;

// Getters and setters
}

@Document
public class Question {

private int questionID;
private String type;

// Getters and setters
}


My JSON doc is like this,



{
"_id" : ObjectId("506d9c0ce4b005cb478c2e97"),
"userId" : 1,
"questions" : [
{
"questionID" : 1,
"type" : "optional"

},
{
"questionID" : 3,
"type" : "mandatory"
}
]
}


I have to update the "type" based on userId and questionId, so I have written a findBy query method inside the custom Repository interface,



public interface CustomRepository extends MongoRepository<PracticeQuestion, String> {

List<PracticeQuestion> findByUserIdAndQuestionsQuestionID(int userId,int questionID);
}


My problem is when I execute this method with userId as 1 and questionID as 3, it returns the entire questions list irrespective of the questionID. Is the query method name valid or how should I write the query for nested objects.



Thanks for any suggestion.










share|improve this question





























    17














    I have two domain objects,



    @Document
    public class PracticeQuestion {

    private int userId;
    private List<Question> questions;

    // Getters and setters
    }

    @Document
    public class Question {

    private int questionID;
    private String type;

    // Getters and setters
    }


    My JSON doc is like this,



    {
    "_id" : ObjectId("506d9c0ce4b005cb478c2e97"),
    "userId" : 1,
    "questions" : [
    {
    "questionID" : 1,
    "type" : "optional"

    },
    {
    "questionID" : 3,
    "type" : "mandatory"
    }
    ]
    }


    I have to update the "type" based on userId and questionId, so I have written a findBy query method inside the custom Repository interface,



    public interface CustomRepository extends MongoRepository<PracticeQuestion, String> {

    List<PracticeQuestion> findByUserIdAndQuestionsQuestionID(int userId,int questionID);
    }


    My problem is when I execute this method with userId as 1 and questionID as 3, it returns the entire questions list irrespective of the questionID. Is the query method name valid or how should I write the query for nested objects.



    Thanks for any suggestion.










    share|improve this question



























      17












      17








      17


      6





      I have two domain objects,



      @Document
      public class PracticeQuestion {

      private int userId;
      private List<Question> questions;

      // Getters and setters
      }

      @Document
      public class Question {

      private int questionID;
      private String type;

      // Getters and setters
      }


      My JSON doc is like this,



      {
      "_id" : ObjectId("506d9c0ce4b005cb478c2e97"),
      "userId" : 1,
      "questions" : [
      {
      "questionID" : 1,
      "type" : "optional"

      },
      {
      "questionID" : 3,
      "type" : "mandatory"
      }
      ]
      }


      I have to update the "type" based on userId and questionId, so I have written a findBy query method inside the custom Repository interface,



      public interface CustomRepository extends MongoRepository<PracticeQuestion, String> {

      List<PracticeQuestion> findByUserIdAndQuestionsQuestionID(int userId,int questionID);
      }


      My problem is when I execute this method with userId as 1 and questionID as 3, it returns the entire questions list irrespective of the questionID. Is the query method name valid or how should I write the query for nested objects.



      Thanks for any suggestion.










      share|improve this question















      I have two domain objects,



      @Document
      public class PracticeQuestion {

      private int userId;
      private List<Question> questions;

      // Getters and setters
      }

      @Document
      public class Question {

      private int questionID;
      private String type;

      // Getters and setters
      }


      My JSON doc is like this,



      {
      "_id" : ObjectId("506d9c0ce4b005cb478c2e97"),
      "userId" : 1,
      "questions" : [
      {
      "questionID" : 1,
      "type" : "optional"

      },
      {
      "questionID" : 3,
      "type" : "mandatory"
      }
      ]
      }


      I have to update the "type" based on userId and questionId, so I have written a findBy query method inside the custom Repository interface,



      public interface CustomRepository extends MongoRepository<PracticeQuestion, String> {

      List<PracticeQuestion> findByUserIdAndQuestionsQuestionID(int userId,int questionID);
      }


      My problem is when I execute this method with userId as 1 and questionID as 3, it returns the entire questions list irrespective of the questionID. Is the query method name valid or how should I write the query for nested objects.



      Thanks for any suggestion.







      mongodb spring-data findby






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 8 '17 at 12:20









      BiJ

      6061928




      6061928










      asked Oct 4 '12 at 15:11









      user1720083

      86113




      86113
























          3 Answers
          3






          active

          oldest

          votes


















          24














          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.
          }





          share|improve this answer























          • Thanks for you reply, but again this query returns an array rather than the appropriate element in the array.
            – user1720083
            Oct 12 '12 at 15:11






          • 1




            Oh. I see what you mean. You can only ever return the entire document. The query actually looks for documents that have that questionID. But you always get the entire document returned - never just the question. It isn't a java thing, it is a MongoDB thing. Look at this question/answer for clarification: stackoverflow.com/a/3985982/229178
            – sbzoom
            Oct 12 '12 at 16:18






          • 1




            After some homework I realized you can specify partial objects in the fields part of your query (projections). mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields I will update my answer above to give an example.
            – sbzoom
            Oct 12 '12 at 16:19










          • Thanks, meanwhile I will also try that.
            – user1720083
            Oct 15 '12 at 7:42



















          10














          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 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 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






          share|improve this answer



















          • 1




            This is quality post
            – ZZ 5
            Apr 23 '18 at 13:58



















          1














          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"
          }
          }





          share|improve this answer





















          • I also tried this but couldn't resolve the problem.
            – WGSSAMINTHA
            Nov 21 '18 at 9:16











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12730370%2fspring-data-mongodb-findby-method-for-nested-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          24














          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.
          }





          share|improve this answer























          • Thanks for you reply, but again this query returns an array rather than the appropriate element in the array.
            – user1720083
            Oct 12 '12 at 15:11






          • 1




            Oh. I see what you mean. You can only ever return the entire document. The query actually looks for documents that have that questionID. But you always get the entire document returned - never just the question. It isn't a java thing, it is a MongoDB thing. Look at this question/answer for clarification: stackoverflow.com/a/3985982/229178
            – sbzoom
            Oct 12 '12 at 16:18






          • 1




            After some homework I realized you can specify partial objects in the fields part of your query (projections). mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields I will update my answer above to give an example.
            – sbzoom
            Oct 12 '12 at 16:19










          • Thanks, meanwhile I will also try that.
            – user1720083
            Oct 15 '12 at 7:42
















          24














          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.
          }





          share|improve this answer























          • Thanks for you reply, but again this query returns an array rather than the appropriate element in the array.
            – user1720083
            Oct 12 '12 at 15:11






          • 1




            Oh. I see what you mean. You can only ever return the entire document. The query actually looks for documents that have that questionID. But you always get the entire document returned - never just the question. It isn't a java thing, it is a MongoDB thing. Look at this question/answer for clarification: stackoverflow.com/a/3985982/229178
            – sbzoom
            Oct 12 '12 at 16:18






          • 1




            After some homework I realized you can specify partial objects in the fields part of your query (projections). mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields I will update my answer above to give an example.
            – sbzoom
            Oct 12 '12 at 16:19










          • Thanks, meanwhile I will also try that.
            – user1720083
            Oct 15 '12 at 7:42














          24












          24








          24






          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.
          }





          share|improve this answer














          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.
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 12 '12 at 16:21

























          answered Oct 10 '12 at 14:04









          sbzoom

          2,27131828




          2,27131828












          • Thanks for you reply, but again this query returns an array rather than the appropriate element in the array.
            – user1720083
            Oct 12 '12 at 15:11






          • 1




            Oh. I see what you mean. You can only ever return the entire document. The query actually looks for documents that have that questionID. But you always get the entire document returned - never just the question. It isn't a java thing, it is a MongoDB thing. Look at this question/answer for clarification: stackoverflow.com/a/3985982/229178
            – sbzoom
            Oct 12 '12 at 16:18






          • 1




            After some homework I realized you can specify partial objects in the fields part of your query (projections). mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields I will update my answer above to give an example.
            – sbzoom
            Oct 12 '12 at 16:19










          • Thanks, meanwhile I will also try that.
            – user1720083
            Oct 15 '12 at 7:42


















          • Thanks for you reply, but again this query returns an array rather than the appropriate element in the array.
            – user1720083
            Oct 12 '12 at 15:11






          • 1




            Oh. I see what you mean. You can only ever return the entire document. The query actually looks for documents that have that questionID. But you always get the entire document returned - never just the question. It isn't a java thing, it is a MongoDB thing. Look at this question/answer for clarification: stackoverflow.com/a/3985982/229178
            – sbzoom
            Oct 12 '12 at 16:18






          • 1




            After some homework I realized you can specify partial objects in the fields part of your query (projections). mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields I will update my answer above to give an example.
            – sbzoom
            Oct 12 '12 at 16:19










          • Thanks, meanwhile I will also try that.
            – user1720083
            Oct 15 '12 at 7:42
















          Thanks for you reply, but again this query returns an array rather than the appropriate element in the array.
          – user1720083
          Oct 12 '12 at 15:11




          Thanks for you reply, but again this query returns an array rather than the appropriate element in the array.
          – user1720083
          Oct 12 '12 at 15:11




          1




          1




          Oh. I see what you mean. You can only ever return the entire document. The query actually looks for documents that have that questionID. But you always get the entire document returned - never just the question. It isn't a java thing, it is a MongoDB thing. Look at this question/answer for clarification: stackoverflow.com/a/3985982/229178
          – sbzoom
          Oct 12 '12 at 16:18




          Oh. I see what you mean. You can only ever return the entire document. The query actually looks for documents that have that questionID. But you always get the entire document returned - never just the question. It isn't a java thing, it is a MongoDB thing. Look at this question/answer for clarification: stackoverflow.com/a/3985982/229178
          – sbzoom
          Oct 12 '12 at 16:18




          1




          1




          After some homework I realized you can specify partial objects in the fields part of your query (projections). mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields I will update my answer above to give an example.
          – sbzoom
          Oct 12 '12 at 16:19




          After some homework I realized you can specify partial objects in the fields part of your query (projections). mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields I will update my answer above to give an example.
          – sbzoom
          Oct 12 '12 at 16:19












          Thanks, meanwhile I will also try that.
          – user1720083
          Oct 15 '12 at 7:42




          Thanks, meanwhile I will also try that.
          – user1720083
          Oct 15 '12 at 7:42













          10














          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 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 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






          share|improve this answer



















          • 1




            This is quality post
            – ZZ 5
            Apr 23 '18 at 13:58
















          10














          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 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 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






          share|improve this answer



















          • 1




            This is quality post
            – ZZ 5
            Apr 23 '18 at 13:58














          10












          10








          10






          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 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 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






          share|improve this answer














          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 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 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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 15 '18 at 12:08









          Sepehr GH

          525419




          525419










          answered Jan 19 '15 at 8:51









          Govardhana Rao Ganji

          32748




          32748








          • 1




            This is quality post
            – ZZ 5
            Apr 23 '18 at 13:58














          • 1




            This is quality post
            – ZZ 5
            Apr 23 '18 at 13:58








          1




          1




          This is quality post
          – ZZ 5
          Apr 23 '18 at 13:58




          This is quality post
          – ZZ 5
          Apr 23 '18 at 13:58











          1














          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"
          }
          }





          share|improve this answer





















          • I also tried this but couldn't resolve the problem.
            – WGSSAMINTHA
            Nov 21 '18 at 9:16
















          1














          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"
          }
          }





          share|improve this answer





















          • I also tried this but couldn't resolve the problem.
            – WGSSAMINTHA
            Nov 21 '18 at 9:16














          1












          1








          1






          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"
          }
          }





          share|improve this answer












          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"
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 26 '17 at 18:36









          Mehraj Malik

          3,60822048




          3,60822048












          • I also tried this but couldn't resolve the problem.
            – WGSSAMINTHA
            Nov 21 '18 at 9:16


















          • I also tried this but couldn't resolve the problem.
            – WGSSAMINTHA
            Nov 21 '18 at 9:16
















          I also tried this but couldn't resolve the problem.
          – WGSSAMINTHA
          Nov 21 '18 at 9:16




          I also tried this but couldn't resolve the problem.
          – WGSSAMINTHA
          Nov 21 '18 at 9:16


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12730370%2fspring-data-mongodb-findby-method-for-nested-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)