Skip to content
Advertisement

How to fetch data using springboot with existing mongodb collection

I have an existing mongo collection called Test collection

{"_id":{"$oid":"61519700c4863cc61e773e65"},"question_id":"Q1","question_category":"MCQ","qustion":"QQQQQQQ1","sequence":1,"status":"ACTIVE","created_by":"","creation_ts":"","last_modified_by":"","last_modofied_ts":"","topic":"Loan History","categories":"Financial","conditions":[],"options":[{"option_id":"Q1_A1","description":"option 1"},{"option_id":"Q1_A2","description":"option 2"},{"option_id":"Q1_A3","description":"option 3"}]}

I have created a model class for the same to fetch the data

package com.example.springbootdatamongodb.model;

import java.util.Arrays;

public class Test {
    private String question_id;
    private String question_category;
    private String qustion;
    private String sequence;
    private String status;
    private String created_by;
    private String creation_ts;
    private String last_modified_by;
    private String last_modofied_ts;
    private String topic;
    private String categories;
    private String[] conditions;
    private String[] options;

    public Test(String question_id, String question_category, String qustion, String sequence, String status, String created_by, String creation_ts, String last_modified_by, String last_modofied_ts, String topic, String categories, String[] conditions, String[] options) {
        this.question_id = question_id;
        this.question_category = question_category;
        this.qustion = qustion;
        this.sequence = sequence;
        this.status = status;
        this.created_by = created_by;
        this.creation_ts = creation_ts;
        this.last_modified_by = last_modified_by;
        this.last_modofied_ts = last_modofied_ts;
        this.topic = topic;
        this.categories = categories;
        this.conditions = conditions;
        this.options = options;
    }

    public String getQuestion_id() {
        return question_id;
    }

    public void setQuestion_id(String question_id) {
        this.question_id = question_id;
    }

    public String getQuestion_category() {
        return question_category;
    }

    public void setQuestion_category(String question_category) {
        this.question_category = question_category;
    }

    public String getQustion() {
        return qustion;
    }

    public void setQustion(String qustion) {
        this.qustion = qustion;
    }

    public String getSequence() {
        return sequence;
    }

    public void setSequence(String sequence) {
        this.sequence = sequence;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCreated_by() {
        return created_by;
    }

    public void setCreated_by(String created_by) {
        this.created_by = created_by;
    }

    public String getCreation_ts() {
        return creation_ts;
    }

    public void setCreation_ts(String creation_ts) {
        this.creation_ts = creation_ts;
    }

    public String getLast_modified_by() {
        return last_modified_by;
    }

    public void setLast_modified_by(String last_modified_by) {
        this.last_modified_by = last_modified_by;
    }

    public String getLast_modofied_ts() {
        return last_modofied_ts;
    }

    public void setLast_modofied_ts(String last_modofied_ts) {
        this.last_modofied_ts = last_modofied_ts;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getCategories() {
        return categories;
    }

    public void setCategories(String categories) {
        this.categories = categories;
    }

    public String[] getConditions() {
        return conditions;
    }

    public void setConditions(String[] conditions) {
        this.conditions = conditions;
    }

    public String[] getOptions() {
        return options;
    }

    public void setOptions(String[] options) {
        this.options = options;
    }


    @Override
    public String toString() {
        return "Test{" +
        "question_id='" + question_id + ''' +
        ", question_category='" + question_category + ''' +
        ", qustion='" + qustion + ''' +
        ", sequence='" + sequence + ''' +
        ", status='" + status + ''' +
        ", created_by='" + created_by + ''' +
        ", creation_ts='" + creation_ts + ''' +
        ", last_modified_by='" + last_modified_by + ''' +
        ", last_modofied_ts='" + last_modofied_ts + ''' +
        ", topic='" + topic + ''' +
        ", categories='" + categories + ''' +
        ", conditions=" + Arrays.toString(conditions) +
        ", options=" + Arrays.toString(options) +
        '}';
    }
}

I have also created TestRepository

package com.example.springbootdatamongodb.repository;

import com.example.springbootdatamongodb.model.Test;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface TestRepository extends MongoRepository<Test,String> {
   List<Test> findBySequence(String sequence);
}

This is my controller method

 @GetMapping("/tutorials/{id}")
 public ResponseEntity<List<Test>> getTutorialById(@PathVariable("id") String id) {
    List<Test> tutorialData = testRepository.findBySequence(id);

    if (tutorialData.isEmpty()) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } else {
        return new ResponseEntity<>(tutorialData,HttpStatus.OK);

    }

}

But I am not able to fetch any data from mongo. I want to fetch data from the mongo collection on the basis of sequence.

I have also tried to add data with the save method

{"_id":{"$oid":"61629f969c9b77073f02cde2"},"question_id":"Q1","question_category":"MCQ","qustion":"QQQQQQQ1","sequence":"1","status":"ACTIVE","created_by":"","creation_ts":"","last_modified_by":"","last_modofied_ts":"","topic":"Loan History","categories":"Financial","conditions":[],"options":[],"_class":"com.example.springbootdatamongodb.model.Test"}

when i inserted the data from java code it added "_class":"com.example.springbootdatamongodb.model.Test" field to the collection and after this i am able to fetch this data, but still existing data i am not able to pull

Advertisement

Answer

There are couple of basic thing u missed. You didn’t tell which collection you are looking for.

@Document(collection="Test") // collection name
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Test {
    private String question_id;
    private String question_category;
    private String qustion;
    private String sequence;
    private String status;
    private String created_by;
    private String creation_ts;
    private String last_modified_by;
    private String last_modofied_ts;
    private String topic;
    private String categories;
    private String[] conditions;
    private String[] options;
}

We use Lombok which reduces the code. You don’t need to generate the long code (getters/ setters/ toString()) as you posted above.

And from the given document the sequence :1 which is an Integer. But you model says its a private String sequence;. Verify it and change the data type according to it.

Advertisement