1

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:

> 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:

@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";
 }
}
2
  • 1
    There is no collection selected on your findOne() method. Since you are calling mongooperations directly without retrieving the collection you need to use that method signature type. Commented May 14, 2014 at 7:25
  • Hi Neil, it's quite not clear for me. Do you mean I should add new parameter (collectionName) to findOne method like this: <T> T findOne(Query query, Class<T> entityClass, String collectionName) Commented May 14, 2014 at 8:02

1 Answer 1

5

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

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.