Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Boot Data MongoDB - How to getting a specific nested object for a super specific object

I have the following data model, and I want to get a specific object in the sub list objects, I know it's possible to get the entire list and go through each object and compare with what the search id, but I wonder if it is possible use MongoRepository to do this.

@Document
public class Host {

    @Id
    private String id;

    @NotNull
    private String name;

    @DBRef
    private List<Vouchers> listVoucher;

    public Host() {
    }

    //Getters and Setters
}

And..

@Document
public class Vouchers {

    @Id
    private String id;

    @NotNull
    private int codeId;

    public Vouchers() {
    }

    //Getters and Setters
}

The Repository Class:

public interface HostRepository extends MongoRepository<Host, String> {

      List<Host> findAll();
      Host findById(String id);
      Host findByName(String name);

      //How to build the correct query ??????????
      List<Vouchers> findVouchersAll();
      Vouchers findByVouchersById(String hostId, String voucherId);  
} 

The Controller Class:

@RestController
@RequestMapping(value = "api/v1/host")
public class VoucherController {

    @Inject
    HostRepository hostRepository;

    @RequestMapping(value = "/{hostId}/voucher",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<Vouchers> list() {
        return hostRepository.findVouchersAll();
    }

    @RequestMapping(value = "/{hostId}/voucher/{voucherId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Vouchers getOneVoucher(@PathVariable String hostId, @PathVariable String voucherId) {

        Vouchers voucher = hostRepository.findByVouchersById(hostId, voucherId);

        if (voucher != null) {

            return voucher;

        } else {

            throw new VoucherNotFoundException(String.format("There is no voucher with id=%s", voucherId));
        }
    }
}

Thanks in Advance!

like image 378
Anderson K Avatar asked May 26 '26 11:05

Anderson K


1 Answers

I think there is a way to do this although I have not tried this myself but maybe I can shed some light in how I would do it.

Firstly, I would rather use the more flexible way of querying mongodb by using MongoTemplate. MongoTemplate is already included in the Spring Boot Mongodb data library and it looks like you are already using the library so it is not an additional library that you will have to use. In Spring there is a way to @Autowired your MongoTemplate up so it is quick and easy to get the object for completing this task.

With mongoTemplate, you would do something like this:

Query query = new Query();
query.addCriteria(Criteria.where("listVouchers.id").is("1234"));
List<Host> host = mongoTemplate.find(query, Host.class);

Please see docs here: https://docs.mongodb.org/manual/tutorial/query-documents/

like image 124
Simon Avatar answered May 30 '26 04:05

Simon