0

I am using maven project with spring-data-mongodb. I created a model class my model class is

@Id
private String _id;

private String message;

private String possibleAnswer;

private boolean resolved;

private String percenteageMatch;
//Getters and Setters

Then i Created a an iterface with

public interface ContextRepository extends MongoRepository<ContextUnknown, String>{}

Now in my controller i want to find all the contextUnkown documents which are not resolved.

I know the query in mongodb is db.getCollection('ContextUnknown').find({"resolved": true}). I am unable to write it in to spring boot i tried adding Creria

Query query = new Query();
query.addCriteria(Criteria.where("resolved").is(false));
return contextRepository.findAll(query)

This is not working as findall doesn't take query as parameter, i tried the same thing with BasicQuery But still the same issue.

I want to get all the elements which are not resolved Is there any thing i should change so i can get all the elements based on query parameters.

Thanks in advance

Any help is deeply appreciated

Santhosh S Kashyap
  • 973
  • 2
  • 13
  • 19

2 Answers2

1

If you want to use Query, you have to use it with MongoTemplate. But because your query is simple, you can(spring-data can) easily build it with spring-data. Add one of these methods in your repository interface;

List<ContextUnknown> findByResolvedIsFalse();

List<ContextUnknown> findByResolved(boolean resolved);

Check this link to learn more about query creation using spring repositories. If you could not build query via spring repositories or you need some aggreation etc, You can combine MongoTemplate and spring repository in one interface. You can check this question's answer to learn how to do it.

barbakini
  • 3,024
  • 2
  • 19
  • 25
0

In the class you are using below code

Query query = new Query();
query.addCriteria(Criteria.where("resolved").is(false));
return contextRepository.findAll(query)

You need MongoTemplate to execute the query.

Do like this

@Autowired
private MongoTemplate mongoTemplate;

List<ContextUnknown> result = mongoTemplate.find(query, ContextUnknown.class);
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85