vasilis 123
vasilis 123

Reputation: 665

find element by title in mongodb with spring boot

This is my first time using Spring Boot and I am a complete beginner . I want to query a mongodb to check if an item has the same title as my input object's title .With the way I have set up my code I cannot find a straightforward way to do this while looking at other solutions .

My code :

BookRepository.java (INITIALIZE MONGODB REPO)

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends MongoRepository<Book,String>{

}

Book.java (ITEM CLASS)

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

@Document(collection="Books")
public class Book{
    
    private String creator;
    private String title;
    
        
    public Book() {
        super();    
    }
    
    
    public Book(String creator ,  String title ) {
        super();
        this.creator=creator;
        this.title=title;
    }
    
    public String getTitle() {
        return "Title is : " + title;
    }
    
    
    
    public String getCreator() {
        return "Creator is : " + creator;
    }
    
}

BookController.java (REST API)

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.backend.Book;
import com.example.backend.BookRepository;

@RestController
public class BookController {
  
    @Autowired
    public BookRepository bookRepo;
    
    @PostMapping(value= "/createBook") 
    public String createBook( @RequestBody Book book){
          
        Query q = new Query();
        q.addCriteria(Criteria.where("title").is(book.getTitle()));
        //what to do now to check if title exists in a document ? 
        
        Memory insertedBook =  bookRepo.insert(book);
        return "Book inserted" +insertedBook.getTitle();    
    }
    
    
}

Upvotes: 0

Views: 1502

Answers (1)

MadTyrael
MadTyrael

Reputation: 125

Add an existsByTitle method to your repository like so. No implementation needed.

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends MongoRepository<Book,String>{
   boolean existsByTitle(String title);
}

You can then call it in your controller:

@PostMapping(value= "/createBook") 
    public String createBook( @RequestBody Book book){
          
        //what to do now to check if title exists in a document ? 
        boolean titleExists = bookRepo.existsByTitle(book.getTitle());
         
        //do something with this information....  
    }

I also recomend that you simplify the output of your getters as they should just return the value and nothing else. If you need to add "Title is : " you can do it later somewhere else:

public String getTitle() {
        return title;
}
    
public String getCreator() {
    return creator;
}

There are plenty of tutorials on youtube and other resources that you can check to get a better grasp of how to spring boot with mongodb works. Check out one of those first, i guarantee you will be able to do stuff like this easily.

Edit: One more thing. Usually you don't connect the controller and the repository directly, if you check out a tutorial you will most likely see the person use an intermediate "service" layer. In your case you would have BookController -> BookService -> BookRepository

With this you can avoid having a ton of logic in your controller which should be as clean as possible.

Ideally your BookController class would look like this:

@RestController
public class BookController {
  
    @Autowired
    public BookService bookService;
    
    @PostMapping(value= "/createBook") 
    public String createBook( @RequestBody Book book){
        return bookService.createBook(book);   
    }
    
    //OTHER CONTROLLER METHODS
}

All the complicated logic like checking if the book title exists and other things can now go to bookService.createBook(). Don't forget to also annotate BookService with @Service

Upvotes: 1

Related Questions