Skip to content

Commit

Permalink
[MongoDB Persistence] Fix date query.
Browse files Browse the repository at this point in the history
Issues:
- Second should have been getEndDate, not getBeginDate
- Send a Date object as the other version wasn't serializable.
- EndDate would have overridden beginDate.

Fixes openhab#10574

Signed-off-by: Stephan Brunner <s.brunner@stephan-brunner.net>
  • Loading branch information
boomer41 committed Apr 26, 2021
1 parent 8f2184a commit 0cc0e7b
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,16 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {
Object value = convertValue(filter.getState());
query.put(FIELD_VALUE, new BasicDBObject(op, value));
}

BasicDBObject dateQueries = new BasicDBObject();
if (filter.getBeginDate() != null) {
dateQueries.put("$gte", Date.from(filter.getBeginDate().toInstant()));
}
if (filter.getBeginDate() != null) {
query.put(FIELD_TIMESTAMP, new BasicDBObject("$lte", filter.getBeginDate()));
if (filter.getEndDate() != null) {
dateQueries.put("$lte", Date.from(filter.getEndDate().toInstant()));
}
if (!dateQueries.isEmpty()) {
query.put(FIELD_TIMESTAMP, dateQueries);
}

Integer sortDir = (filter.getOrdering() == Ordering.ASCENDING) ? 1 : -1;
Expand Down

0 comments on commit 0cc0e7b

Please sign in to comment.