Multiple errors in custom spring-data query with nullable parameters

Viewed 17

I have a query that takes 3 parameters. It should check whether these parameters are null before assigning them the actual value and perform the query. I do this because if all parameters are null, then everything is null in the query and it returns everything from the collection. The query is the following.

@Query("{ $or: [ { $or: [ { $where: '?0 == null' }, { 'userId': ?0 } ] }, { $or: [ { $where: '?1 == null' }, { $where: '?2 == null' }, { 'timestamp': { $gte: ?1, $lte: ?2 } } ] } ] }")
  List<Location> findLocation(String userId, Date from, Date to);

Although it works as expected if everything is null, when I give it something that is not in the collection it crashes with ReferenceError: marioss is not defined :\n@:1:15\n' on server ...; (where the userId in the single document that is present in the collection is marios) and if I give it a date I get this error 'SyntaxError: identifier starts immediately after numeric literal'.

0 Answers
Related