Spring Data MongoDB : like query example
In SQL, the ‘like’ query is looks like this :
select * from tags where tagName like '%apple%'
In MongoDB console, it looks like this :
db.tags.find({"tagName": /apple/})
In Spring data mongodb, it implements with Criteria
or BasicQuery
:
String tagName = "apple";
Query query = new Query();
query.limit(10);
query.addCriteria(Criteria.where("tagName").regex(tagName));
mongoOperation.find(query, Tags.class);
String tagName = "apple";
BasicQuery query =
new BasicQuery("{\"tagName\": {$regex : '" + tagName + "'} }");
query.limit(10);
mongoOperation.find(query, Tags.class);
how to implement in and like query together
What about something like the below?
SELECT * FROM Person WHERE firstname like '%tom%' OR lastname like '%tom%'
How i can write query using Criteria for expression like /^stringValue/