Akash gupta
Akash gupta

Reputation: 11

how to call MongoTemplate function on Autowired instance of MongoTemplate in Spring boot

I have configured the MongoDB connection in spring boot. Now I want to run a custom query by using MongoTemplate find() method. but spring boot is not allowing me to do so.

@Autowired
MongoTemplate mongoTemplate;

public List<UserVO> findByQuery(String query) {
    List<UserVO> users = mongoTemplate.find(query, UserVO.class);
    return users;
}

mongoTemplate is autoconfigured with mongo URI, collection name is defined in UserVO.class using @Document.

Error: The method find(Query, Class) in the type MongoOperations is not applicable for the arguments (String, Class)

Any suggestion would be highly appreciated.

Upvotes: 0

Views: 786

Answers (1)

nestdimmy
nestdimmy

Reputation: 71

As per specification mongoTemplate.find(Query query, Class entityClass) has such parameters

You need to specify Query like

new Query(Criteria.where("fieldName").is("searchValue")

Upvotes: 1

Related Questions