Reputation: 1265
I am using MongoDB with Spring. I want to query the database by _id.
Currently I have this query:
Query q = new Query(Criteria.where("_id").is(someId).and("deleted").is(false));
But this is giving me StackOverflowError somehow. What's wrong with this query? Or what is a better way of doing this?
Upvotes: 1
Views: 1328
Reputation: 103305
Create an explicit AND query instead of a chained one using the $and
operator Criteria.andOperator()
for all of the provided criteria as follows:
Query q = new Query(
new Criteria().andOperator(
Criteria.where("_id").is(someId),
Criteria.where("deleted").is("false")
)
);
This is normally used in instances where you can't use Criteria.and()
to add multiple criteria into the same field, for example
Query q = new Query();
q.addCriteria(Criteria.where("age").lt(40).and("age").gt(10));
will throw an error, so a workaround would be to use Criteria.andOperator()
as
Query q = new Query();
q.addCriteria(
Criteria.where("age").exists(true).andOperator(
Criteria.where("age").gt(10),
Criteria.where("age").lt(40)
)
);
Upvotes: 3