如何使用带有现有 mongodb 集合的 spring boot 获取数据

How to fetch data using springboot with existing mongodb collection

我有一个名为测试集合的现有 mongo 集合

{"_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"}]}

我已经为它创建了一个模型class来获取数据

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) +
        '}';
    }
}

我也创建了 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);
}

这是我的控制器方法

 @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);

    }

}

但我无法从 mongo 获取任何数据。我想根据顺序从 mongo 集合中获取数据。

我也试过用save方式添加数据

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

当我从 java 代码中插入数据时,它向集合中添加了 "_class":"com.example.springbootdatamongodb.model.Test" 字段,之后我能够获取这些数据,但仍然存在我无法提取的数据

您错过了一些基本的东西。你没有告诉你要找哪个合集。

@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;
}

我们使用 Lombok 来减少代码。您不需要像上面发布的那样生成长代码 (getters/setters/toString())。

并且从给定的文档中 sequence :1 这是一个 Integer。但是你的模型说它是 private String sequence;。验证它并根据它更改数据类型。