Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Morphia mongoDB wildcard query

Straight forward question, does anybody know how to make a wildcard query using morphia linked to a mongoDB database?

This is what the mongo statement would look like:

Mongo: db.users.find({name:/Joe/})
SQL: SELECT * FROM users WHERE name LIKE "%Joe%"

My morphia statement looks like:

Morphia: ds.find(File.class, "filename","/test/").order("filename").asList();

I have filenames in my database such as test1, test etc

If someone could please tell me if it's even possible with morphia it would be most appreciated.

Thanks

like image 515
user1479897 Avatar asked Sep 11 '12 13:09

user1479897


Video Answer


3 Answers

What you refer to as a "wildcard" is in fact a "Regular Expression".

The Java class which represents regular expressions is the Pattern. You can pass these to the filter method of Morphia's Query object.

// create a regular expression which matches any string which includes "test"
Pattern regexp = Pattern.compile("test");
// use this regular expression to create a query
Query q = ds.createQuery(File.class).filter("filename", regexp).sort("filename");
like image 174
Philipp Avatar answered Sep 22 '22 14:09

Philipp


This will also work

DS.find(Model.class).field("filename").startsWithIgnoreCase("name").asList();
like image 21
Indrajeet Avatar answered Sep 22 '22 14:09

Indrajeet


Can also do:

ds.createQuery(File.class)
    .criteria("filename").contains("test")
    .asList();
like image 36
Ali Saeed Avatar answered Sep 21 '22 14:09

Ali Saeed