简体   繁体   中英

Spring Boot MongoDB get only certain fields from database into model

I am trying to understand how to work with MongoDB and Spring Boot. I have a mongodb with sample data and I want it to return only selected fields and not the entire data.

My mongoDB collection contains data in the format

在此处输入图像描述

DAO.java

package com.books.Reading.DAO;
import com.books.Reading.model.booksModel;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface readingListDAO extends MongoRepository<booksModel, String> {

    @Query("{'title' : 'Griffon in Action'}")
    public booksModel findFirstByOrderByTitleAsc();
}

model.java

package com.books.Reading.model;

import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class booksModel {
    String title;
    String isbn;
    int pageCount;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
}

controller.java

package com.books.Reading.controller;

import com.books.Reading.model.booksModel;
import com.books.Reading.service.readingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class readingListController {

    @Autowired
    private readingListService rlService;

    @RequestMapping("/")
    @ResponseBody
    public booksModel getOne(){
        return rlService.getOne();
    }

}

service.java

package com.books.Reading.service;

import com.books.Reading.model.booksModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class readingListService {

    @Autowired
    private com.books.Reading.DAO.readingListDAO readingListDAO;

    public booksModel getOne(){
        System.out.println(readingListDAO.findFirstByOrderByTitleAsc());
        return readingListDAO.findFirstByOrderByTitleAsc();
    }
}

application.properties

spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=book
spring.session.mongodb.collection-name=readinglist

The MongoDB is hosted locally. How can I retrieve only selected fields (title, isbn and pageCount) from the database.

I am very new to Spring Boot, so if there are other errors or best practices, I welcome your feedback.

By default, all the properties of document will be fetched. if you want to ignore some of the properties in the result, you can use filter option.

        @Query(value = "{'title' : ?0}", fields = "{'description' : 0}")
        BooksModel findBooksModelByTitle(String title);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM