gpt4 book ai didi

具有长类型_id的java mongodb findOne

转载 作者:可可西里 更新时间:2023-11-01 10:43:39 24 4
gpt4 key购买 nike

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com