3

I have a field in my mongodb called "name". I am using annotations in spring data to support querying. My question is, is there a way to support wildcard? i.e. If I have values for "name" called "Robert", "Roberto" "Ramano", I could support queries that allow me to pass say "R" to a function, and it will match on everything that starts with R? Right now I have to basically do an "exact spelling" of Robert, or any one of those names to get an exact match.

I know how to do wildcard search with mongodb directly, but am not sure how to do it in java with spring data. I have a model class representing my Student documents that I use annotations to describe how to query.

db.users.find({"name": /.*m.*/})

I don't know how to translate that into java as I want to pass in a variable. For example:

Pseudocode:

String myvar = "R";
db.users.find({/.*<variable here>*/})

The following is what I have on my "MongoRepository" implementation:

public interface UserRepository extends MongoRepository<UserId, String> {
{
    @Query("{'name' : {$regex : ?0}}")
    public List<Users> findByName(String username);
}

When I pass in the full name "Robert", then it is able to find "Robert". However, if I put "R", it does not find anything.

Rolando
  • 58,640
  • 98
  • 266
  • 407
  • possible duplicate of [How to query mongodb with "like"?](http://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like) – Blakes Seven Aug 28 '15 at 04:50
  • Basically anything with a regular expression or possibly and/or the `$in` operator, which also supports regular expressions in addition to a list of exact arguments. – Blakes Seven Aug 28 '15 at 04:52
  • 1
    @BlakesSeven Not a duplicate. This question is specifically about Spring Data. – chrylis -cautiouslyoptimistic- Aug 31 '15 at 13:48
  • @chrylis So what? The end result is the same, which is "use a regular expression". And the next time you decide to get so "high and mighty" then please read the revision history and realize the duplicate was marked "before" all of the revisions made to the question to put it into it's current form. Now it may not deserve immediate closure depending on whether there is a reasonable outcome. At which time I may reconsider. Okay by you? Also noting "zero" communication from the OP. Which is something that could be a lot better. – Blakes Seven Aug 31 '15 at 13:58
  • 2
    The original question without any revision contained specifically "but am not sure how to do it in java with spring data", "I am using annotations in spring data to support querying. " – Rolando Aug 31 '15 at 14:04

2 Answers2

8

Did you try it with query method?

public interface UserRepository extends MongoRepository<UserId, String> {
{

    public List<Users> findByNameLike(String username);

}
loc
  • 374
  • 3
  • 11
  • YES!!! This is exactly what I've been looking for. Bless you for reading documentation and bravo to Spring for implementing such magic. – heez Nov 24 '17 at 19:44
0

Why dont you use regex like you're trying to in the following manner :

public interface UserRepository extends MongoRepository<UserId, String> {
{
    @Query("{'name' : {$regex : ?0}}")
    public List<Users> findByName(String regexp);
}

and in your service form a query somewhat like this :

@Mock
UserRepository userRepository; 

String queryInput = "^R.*$";
List<Users> users = userRepository.findByName(queryInput);

find more detailed answers in here in section 4.2

fireball.1
  • 1,413
  • 2
  • 17
  • 42