Multiple or-clause

277 views
Skip to first unread message

Achim Hilwers

unread,
Mar 31, 2012, 12:39:31 PM3/31/12
to mor...@googlegroups.com
Hello!

I'm currently using the following code to create a query:

protected Query<Contact> buildQuery(Company company, String filter) {
Query<Contact> contactQuery = HiWebSuite.getDatastore().createQuery(Contact.class);
contactQuery.field("company").equal(company);
if (filter!=null) {
contactQuery.or(
contactQuery.criteria("firstName").containsIgnoreCase(filter),
contactQuery.criteria("name1").containsIgnoreCase(filter),
contactQuery.criteria("name2").containsIgnoreCase(filter),
contactQuery.criteria("name3").containsIgnoreCase(filter),
contactQuery.criteria("street").containsIgnoreCase(filter),
contactQuery.criteria("zip").containsIgnoreCase(filter),
contactQuery.criteria("city").containsIgnoreCase(filter)
);
}
contactQuery.or(
contactQuery.criteria("deleted").doesNotExist(),
contactQuery.criteria("deleted").equal(false)
);
contactQuery.order("name1");
return contactQuery;
}

Query is a normal com.google.code.morphia.query.Query.

I added the second "or" because I have documents in my collections that don't have the "deleted"-field yet. 

As long as the filter is not set and the first "or" is not invoked, all works fine but if the first "or" is invoked, the query returns an empty result. The filter itself works because if I remove the second "or", all works fine again.

In this case I can remove the second "or" and set the "deleted" in all my documents but isn't it possible to use more than one "or" in a query?

Regards,

Achim

Scott Hernandez

unread,
Mar 31, 2012, 3:10:40 PM3/31/12
to mor...@googlegroups.com
You need to use and() explicitly: See this issue
http://code.google.com/p/morphia/issues/detail?id=338
Reply all
Reply to author
Forward
0 new messages