I am trying to use make my own query for a mongo Repository:
@Repository
public interface LogEntryRepository extends MongoRepository<LogEntry,String> {
@Query("{'created_at' : {{ $gte: ISODate(?0)},{$lt: ISODate(?1)}}, " +
"$or: [{'site': {$regex: ?2}}, {'login': {$regex: ?2}}, {'ip': {$regex: ?2}} ]" +
"}")
public Page<LogEntry> findByDateTimeBetweenAndCriteria(String isoStartDate, String isoEndDate, String searchTerm, Pageable page);
}
What I'd like to achieve is searching though dated logs with a keyword. The above complains about a parse error:
Caused by: com.mongodb.util.JSONParseException:
{'created_at' : { $gte: ISODate("_param_0"), $lt: ISODate("_param_1")}, $or: [{'site': {$regex: "_param_2"}}, {'login': {$regex: "_param_2"}}, {'ip': {$regex: "_param_2"}} ]}
^
If I replace the ISODate(?0)
with simply ?0
it produces Page 1 of 0 containing UNKNOWN instances
The Strings isoStartDate
& isoEndDate
are produced from java.util.Date
and look like this 2017-06-27T00:00:00.000Z
How do I get my date in there?
ISODate is a Mongo shell construct to create a BSON date and definitely not valid JSON, which is what I believe your error is complaining about.
Try replacing the above ISODate calls with
{ '$date' : '?0' }
and{ '$date' : '?1' }
as suggested in this answer. All the strings should probably need to be surrounded in single quotes.