Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data - Mongodb - findBy Method for nested objects

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.

like image 928
user1720083 Avatar asked Oct 04 '12 15:10

user1720083


People also ask

How do I query a nested object in MongoDB?

To specify a query condition on fields in an embedded/nested document, use dot notation ( "field. nestedField" ).

What is the difference between MongoOperations and MongoTemplate?

MongoTemplate provides a simple way for you to save, update, and delete your domain objects and map those objects to documents stored in MongoDB. You can save, update and delete the object as shown below. MongoOperations is the interface that MongoTemplate implements.

How do I join two MongoDB collections in spring boot?

For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.

How does spring boot store JSON objects in MongoDB?

To store raw json object/array, all you have to do is to declare the type as "Object" in the Pojo and/or DTO level on your server side. The "Object" type will work with Spring Data and MapStruct too. Then on the client side, you can send your json data as a json data.


1 Answers

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. } 
like image 173
sbzoom Avatar answered Sep 23 '22 13:09

sbzoom