Main Tutorials

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);

References

  1. SQL to MongoDB Mapping Chart
  2. MongoDB $regex
  3. Spring Data MongoDB : Query Document

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Manmath Waghmare
5 years ago

how to implement in and like query together

Kenneth
3 years ago

What about something like the below?

SELECT * FROM Person WHERE firstname like '%tom%' OR lastname like '%tom%'

Prashant Thorat
10 years ago

How i can write query using Criteria for expression like /^stringValue/