简体   繁体   English

使用spring和mongodb根据我的查询获取文档列表

[英]Get List Of documents based on my query using spring and mongodb

My Model Class look like this: 我的模型类如下所示:

 package io.springBoot.ProductionDetails;
    import org.springframework.data.annotation.Id;

    public class ProductionDetailsModel {

    //Created an entity for ProductDetails
    @Id
     private String productId;
     private String name;
     private String description;
     private String image; //Alway's in Binary data 
     private String sellerUserId;


    public ProductionDetailsModel() {

    }

    //Constructors
    public ProductionDetailsModel( String productId, String name, String description, String image, String sellerUserId) {
        super();
        this.productId = productId;
        this.name = name;
        this.description = description;
        this.image = image;
        this.sellerUserId = sellerUserId;
    }

    //Getter and Setter Methods for objects
    public String getProductId() {
        return productId;
    }
    public void setProductId(String productId) {
        this.productId = productId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getSellerUserId() {
        return sellerUserId;
    }
    public void setSellarUserId(String sellerUserId) {
        this.sellerUserId = sellerUserId;
    }



I am able to get single document based on this query:

    public void getProductDetails(String Id) {
        return productdetialsRepository.findOne(id);    
    }   

I want to pass a perameter like: sellerUserId and get all the documents based on sellerUserId form the Mongodb. 我想通过一个像perameter一样的内容:SellerUserId并从Mongodb中获取所有基于SellerUserId的文档。 How can i achieve this? 我怎样才能做到这一点? Any Help please? 有什么帮助吗?

Add a method like this in ProductDetailsRepository 在ProductDetailsRepository中添加这样的方法

 public interface ProductdetialsRepository extends MongoRepository<ProductionDetailsModel, String>{ 
    List< ProductionDetailsModel> findAllBySellerUserId(final String  sellerUserId); 
    }

In my Controller I added 在我的控制器中,我添加了

@RequestMapping(method=RequestMethod.GET, path = "/{sellerUserId}")
    void getProductDetails(@PathVariable String sellerUserId) {
    System.out.println(sellerUserId);
    productdetialsRepository.findAllBySellerUserId(sellerUserId);
    }

and also i done experiment with this 我也做了这个实验

Query query = new Query();
query.addCriteria(Criteria.where("id").is(sellerUserId));
List<ProductionDetailsModel> productionDetailsList =    productdetialsRepository.find(query ProductionDetailsModel.class);

But i am getting null pointer Exception any one can please help me? 但是我得到空指针异常有人可以帮助我吗?

You need to write method in your Repository Interface 您需要在存储库接口中编写方法

List< ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId);

and call this method in your serviceImpl class 并在您的serviceImpl类中调用此方法

List<ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId){
    return productdetialsRepository.findBySellerUserId(sellerUserId); 
}

And other better solution as you are using mongo with spring is to use mongoOperations(which is provided by spring framework) 当您在Spring中使用mongo时,其他更好的解决方案是使用mongoOperations(由spring框架提供)

Query query = new Query();
query.addCriteria(Criteria.where("id").is(sellerUserId));
List<ProductionDetailsModel> productionDetailsList = mongoOperation.find(your query, ProductionDetailsModel.class);

Add a method like this in ProductDetailsRepository 在ProductDetailsRepository中添加这样的方法

List<ProductDetails> findBySellerUserId(String sellerUserId)

Spring data will formulate the query based on method name and get the result. Spring数据将基于方法名称制定查询并获得结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Spring MongoOperations获取基于查询的所有文档 - Get all documents based on query using Spring MongoOperations 获取按集合属性长度排序的 MongoDB 文档列表 - Spring Boot - Get list of MongoDB documents sorted by collection property length - Spring Boot 如何使用Spring Data MongoDB查询无效引用的文档 - How to query for documents with invalid references using spring data mongodb 使用Spring-Integration获取具有某些字段(投影)的mongodb文档(仅限注释) - Get mongodb documents with certain fields(projections) using Spring-Integration (annotations only) 使用Spring MongoRepository根据相同查询参数的值的不同组合获取所有Mongo文档 - Fetching all Mongo documents based on different combinations of values of same query parameters using Spring MongoRepository Spring MongoDB查询文件,如果天差为x天 - Spring MongoDB query documents if days difference is x days Spring Data MongoDB:一种插入或更新文档列表的方法(upsertAll?) - Spring data mongodb: A way to insert or update a list of documents (upsertAll?) 如何使用Spring Data MongoDB将文档嵌入到Map中? - How do I embed documents in a Map using Spring Data MongoDB? 来自MongoDB的随机文档使用spring-data - Random documents from MongoDB using spring-data 使用Spring Data MongoDB中的List参数进行存储库查询 - Repository query with a List parameter in Spring Data MongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM