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
author image

mkyong

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. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Manmath Waghmare
Guest
Manmath Waghmare

how to implement in and like query together

Prashant Thorat
Guest
Prashant Thorat

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