2

I have the following filter for my mongodb:

"{'shortname': '/.*LKH.*/'}"

I use it with the following java code:

  BasicQuery c = new BasicQuery(filter);
  Iterable<Hospital> hospitals = template.find(c,Hospital.class);

I am getting no results, because of the surounding ' at the regular expression. If i execute the filter without the '' around the regex, i get results in mongodb. I tried different verisons but could not succeed. The filter i´m appliying must be generic, so i cannot have some parsing.

Has someone an idea how i can use a generic filter for MongoDB with the MongoTemplate in Java - or how i have to write my filter?

Stephan
  • 41,764
  • 65
  • 238
  • 329
kamahl
  • 931
  • 1
  • 8
  • 20
  • You have to use regex patterns, have a look at this question: http://stackoverflow.com/questions/5608584/how-to-query-mongodb-with-like-using-the-java-api – CodeFox Apr 18 '15 at 15:07
  • I´ve seen this answear but it's not what i´m looking for. I want to be able to apply a string as a filter, so it might also be any other filter. – kamahl Apr 18 '15 at 16:52
  • I've never used mongodb the way you use it. But maybe you want to try [$regex](http://docs.mongodb.org/manual/reference/operator/query/regex/) like in `"{'shortname': {$regex: /.*LKH.*/}}"` – Wolfgang Kluge May 22 '15 at 18:19

1 Answers1

0

Solution proposed by Wolfgang Kluge:

I've never used mongodb the way you use it. But maybe you want to try $regex like in
"{'shortname': {$regex: /.*LKH.*/}}"

Community
  • 1
  • 1
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • 1
    hi, we used a quite strange solution back then. We posted a javascript evaluation. As it was just a uni-project with limited time at hand, we needed a quick solution, although i would never apply this solution in real world – kamahl Aug 26 '15 at 10:21