我的 mongodb 集合中的 ids 格式如下
"_id" : NumberLong("502434721406525440"),
我正在尝试执行 mongo 的 findOne 方法来检索具有所需 ID 的项目。但是,我无法使用以下代码片段进行管理。
@Autowired
private MyItemRepository myRepo;
public MyItem getItemById(String itemId) {
return myRepo.findOne(itemId);
}
和MyItem类如下:
public class MyItem{
@Id
private final long itemId;
//and some other fields
//and getter methods
}
我的仓库如下:
public interface MyItemRepository extends
PagingAndSortingRepository<MyItem, String> {
}
提前致谢。
最佳答案
重写 findOne 方法解决了我的问题。我可以帮助遇到同样问题的任何人。
mongoTemplate.findOne(
Query.query(Criteria.where("itemId").is(Long.decode(itemId))),
MyItem.class, collectionName);
关于具有长类型_id的java mongodb findOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25644149/