findAll(Iterable <e>元素)在mongo存储库中不起作用

时间:2017-05-17 09:03:45

标签: java mongodb spring-mongo mongorepository

这是我的mongo存储库

@Repository
public interface StockRepository extends MongoRepository<Stock,String> {
    Stock findStockByStockSymbol(String stockSymbol);
}

这是我的Stock班级

@Document(collection="stocks")
public class Stock {

    @Id
    private String stockSymbol;
    private String StockName;

    private Double basePrice;


    public String getStockSymbol() {
        return stockSymbol;
    }

    public void setStockSymbol(String stockSymbol) {
        this.stockSymbol = stockSymbol;
    }

    public String getStockName() {
        return StockName;
    }

    public void setStockName(String stockName) {
        StockName = stockName;
    }

    public Double getBasePrice() {
        return basePrice;
    }

    public void setBasePrice(Double basePrice) {
        this.basePrice = basePrice;
    }

    @Override
    public String toString() {
        return "Stock{" +
                "stockSymbol='" + stockSymbol + '\'' +
                ", StockName='" + StockName + '\'' +
                ", basePrice=" + basePrice +
                '}';
    }
}

我正在尝试从Stock

列表中获取stockSymbols列表
stockRepository.findAllStockByStockSymbol(symbolList);

但它返回一个空列表。我做错了什么?

1 个答案:

答案 0 :(得分:1)

显然spring-mongo无法理解我的查询。我通过使用Query注释指定自定义查询来实现它。

@Repository
public interface StockRepository extends MongoRepository<Stock,String> {
    @Query("{ 'stockSymbol' : { $in : ?0 } }")
    List<Stock> findAllStockByStockSymbol(List<String> stockSymbol);

}