我正在尝试运行一个简单的搜索查询,但它返回一个空数组,数据库中大约有三条记录。我还有其他查询,它们都运行正常。有人能给我指出正确的方向吗?
模型
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "order")
public class OrderDTO {
@Id
private String ordId;
private List<Category> categoryName;
}
存储库
@Repository
public interface OrderRepository extends MongoRepository<OrderDTO, String> {
@Query("{'categoryName': {'$regex': ?0 }})")
public List<OrderDTO> findByCategoryName(String categoryName);
}
服务
public List<OrderDTO> searchOrderByCategoryName(String categoryName){
return or.findByCategoryName(categoryName);
}
控制器
@RestController
@RequestMapping("/portal")
@CrossOrigin(origins = "http://localhost:3000")
public class OrderController {
@Autowired OrderService os;
@GetMapping("/search/{categoryName}")
public List<OrderDTO> searchOrderByCategoryName(@PathVariable(value = "categoryName") String categoryName) {
return os.searchOrderByCategoryName(categoryName);
}
数据库
{
"_id": {
"$oid": "678ed41ceac5b05ec1a2dea8"
},
"categoryName": [
{
"categoryName": "N"
}
],
"productName": [
{
"productName": "9999999"
},
{
"productName": "888888"
},
{
"productName": "77777777"
},
{
"productName": "55555555"
}
],
"productQuantity": 90,
"ordUpdated": {
"$date": "2025-01-20T22:54:20.729Z"
},
"isInStock": false,
"_class": "com.softoffice.portal.model.OrderDTO"
}