Results 1 to 4 of 4

Thread: Spring mongodb support: hot to write a like query

  1. #1
    Join Date
    Jul 2007
    Location
    Italy
    Posts
    61

    Default Spring mongodb support: hot to write a like query

    Hi All

    I'm using Spring 3.1 and Spring mongo support

    I'ld like to have some sample how to write a like query; i'ld like to write, by using the spring Criteria and Query API for mongo, somethink similar to this SQL query:
    Code:
    select * from user where name like '%mart%'
    The bold part is what is interesting for me.....do u have any suggestion? Also fro making it case insensitive

    Thank you so much
    Cheers
    Angelo

  2. #2
    Join Date
    Jul 2007
    Location
    Italy
    Posts
    61

    Default

    Hi there

    I solved this question.
    I used the following code:

    Code:
    Criteria.where("raw").regex(value,"i");
    Where raw is the name of the field where I want to do the like and regex is the Criteria method to use.

    The second parameter ("i") represents the options to pass to the method; in this case "i" tells mongodb to be case insensitive.

    Cheers,
    Angelo

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    Default

    Hello Angelo,

    I have a similar concern.

    What is there in the value , does it contains '%mart%'


    Regards,
    Suraj Sharma

  4. #4
    Join Date
    Jul 2007
    Location
    Italy
    Posts
    61

    Default

    Hi Suraj

    Nope in the value there is simply the value; for example if you need to search for "%mart%" you have to do:
    Code:
    String value = "mart";
    Criteria.where("raw").regex(value,"i");
    I hope this is useful

    Angelo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •