Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb criteria query for primary key _id?

I write the criteria query like this

query.addCriteria(Criteria.where("_id").is(id)); 

but the result is {"_id":123456789123450} which is not searching result. What I need to change to search the result using the criteria query on _id?

like image 253
Prabhat Yadav Avatar asked Jan 11 '18 18:01

Prabhat Yadav


1 Answers

There are two ways to do this

Query query = new Query(Criteria.where("id").is(new ObjectId(<id value>)));

Or Spring data MongoDB's findById

org.springframework.data.repository.CrudRepository.findById(String id);

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html#findById-ID-

like image 54
Debopam Avatar answered Nov 15 '22 05:11

Debopam