Java 在 Spring Data (MongoDB) 中实现 findOne

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23646983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:30:31  来源:igfitidea点击:

Implementing findOne in Spring Data (MongoDB)

javamongodbspring-mvcspring-data-jpa

提问by Iurii

I have some problems with executing findOne method of MongoOperations class, for now this method return null. My data structure in mongoDB is looking like this:

我在执行 MongoOperations 类的 findOne 方法时遇到了一些问题,现在这个方法返回 null。我在 mongoDB 中的数据结构如下所示:

> db.news.find({_id:1})
{ "_id" : 1, "title" : "first title", "text" : "first text" }
> db.news.find({_id:{$type:1}})
{ "_id" : 1, "title" : "first title", "text" : "first text" }

As you can see above _id field has Double type. My Java classes is looking like this:

正如您在上面看到的 _id 字段具有 Double 类型。我的 Java 类看起来像这样:

@Repository
public class NewsService {

    @Autowired
    private MongoOperations mongoOperations;

    public static final String COLLECTION_NAME = "news";

    //this method executes ok
    public List<NewsEntity> getAllNews() {
        return mongoOperations.findAll(NewsEntity.class, COLLECTION_NAME);
    }

    //but this method return null     
    public NewsEntity getNewsDetail(Long id) {
        return mongoOperations.findOne(Query.query(Criteria.where("_id").is(id)), NewsEntity.class);
    }

Entity class:

实体类:

@Document
public class NewsEntity {

 @Id
 private Long id;
 private String title;
 private String text;


 public Long getId() {
     return id;
 }

 public void setId(Long id) {
     this.id = id;
 }

 public String getTitle() {
     return title;
 }

 public void setTitle(String title) {
     this.title = title;
 }

 public String getText() {
     return text;
 }

 public void setText(String text) {
     this.text = text;
 } 
}

And Spring controller:

和弹簧控制器:

@Controller
public class MainController {
 @Autowired
 private NewsService newsService;

 @RequestMapping(value="/news/details/{newsId}",method = RequestMethod.GET)
 public String getNewsDetails(ModelMap model, @PathVariable("newsId") Long newsId) {
     //newsEnt is null here...
     NewsEntity newsEnt = newsService.getNewsDetail(newsId);

     model.addAttribute("newsDet", newsEnt);
     return "newsdetails";
 }
}

采纳答案by Neil Lunn

You are calling the mongoOperationsinstance directly and not first retrieving a collection. So much like the findAllmethod you have implemented you also need the form that contains the collection as an argument:

您是mongoOperations直接调用实例而不是首先检索集合。与findAll您实现的方法非常相似,您还需要包含集合作为参数的表单:

public NewsEntity getNewsDetail(Long id) {
    return mongoOperations.findOne(
        Query.query(Criteria.where("_id").is(id)),
        NewsEntity.class,
        COLLECTION_NAME
    );
}

This is covered in the documentation for findOne, also see the available method signatures in the summary.

这在 的文档中进行了介绍findOne,另请参阅摘要中的可用方法签名。