Amila Iddamalgoda
Amila Iddamalgoda

Reputation: 4286

Java + Mongo +Morphia - Finding Regex Expression in mongo array

I have a mongo query as follows.

db.Course.find( { $and: [{courseCallNo: {$in : [/^ssoapicall1$/i]} }, {clientId :123456 } ] })

In my java program , I use jmkgreen/morphia as my mongo library. I need to get the exact match as above using java. Following is my code. Please help me out to build this. Thanks.

PS : I guess the problem is , I'm using a string array . I need to create array with regular expressions.

 String courseCallNoRegex = "/^"+originalCourseCallNo+"$/i";        
    List<String> courseCallNoList = new ArrayList<String>();
    courseCallNoList.add(courseCallNoRegex);    
    courseCallNoList.add(courseCallNoRegex);    
    List<Course>  courses= getDataStore().createQuery(Course.class).field(Constants.Course.COURSE_CALL_NO).in(courseCallNoList).field(Constants.Course.CLIENT_ID).equal(clientId).asList();

Upvotes: 0

Views: 364

Answers (1)

xeraa
xeraa

Reputation: 10859

  1. The jmkgreen fork is really old. I'd definitely try to get onto the official MongoDB Morphia version, which includes a lot of bugfixes and new features.

  2. This... will... be... slow...
    Unless the collection is really small, you'll soon feel the pain of this. If you cannot normalize the data (call a .toLowerCase() in the @PrePersist on it), I'd duplicate the data into something like courseCallNoNormalized. So you can use exact matches, ideally with an index.

  3. Have you tried the following?

    Pattern regexp =
       Pattern.compile("^" + originalCourseCallNo + "$", Pattern.CASE_INSENSITIVE);
    mongoDatastore.find(Course.class)
       .field(Constants.Course.CLIENT_ID).equal(clientId)
       .filter(Constants.Course.COURSE_CALL_NO, regexp).asList();
    

Upvotes: 1

Related Questions