Spring Data MongoDB : like query example
By:Roy.LiuLast updated:2019-08-17
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);
References
From:一号门
COMMENTS