Morphological query Morphia mongoDB

A direct-directed question, does anyone know how to make a template query using the morphine associated with the mongoDB database?

Here is what the mongo expression should look like:

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

My application for morphine looks like this:

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

I have file names in my database such as test1, test, etc.

If someone can tell me if this is possible with morphine, that would be very helpful.

thanks

+6
source share
3 answers

What you call a "wildcard" is actually a "Regular Expression" .

The Java class that represents regular expressions is Pattern . You can pass them to the filter method of the Morphia 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"); 
+20
source

It will also work

 DS.find(Model.class).field("filename").startsWithIgnoreCase("name").asList(); 
+2
source

You can also:

 ds.createQuery(File.class) .criteria("filename").contains("test") .asList(); 
0
source

Source: https://habr.com/ru/post/925133/


All Articles