这是我的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);
但它返回一个空列表。我做错了什么?
答案 0 :(得分:1)
显然spring-mongo无法理解我的查询。我通过使用Query
注释指定自定义查询来实现它。
@Repository
public interface StockRepository extends MongoRepository<Stock,String> {
@Query("{ 'stockSymbol' : { $in : ?0 } }")
List<Stock> findAllStockByStockSymbol(List<String> stockSymbol);
}