Following is an example of a QueryBuilder.
public List<Map<String, Object>> findList() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
BasicDBObject document = new BasicDBObject();
Calendar cal = Calendar.getInstance();
Date now = cal.getTime();
QueryBuilder qb = new QueryBuilder();
qb.or(new QueryBuilder().put("starting_date").is(null).put("ending_date").is(null).get(),
new QueryBuilder().put("starting_date").lessThanEquals(now).put("ending_date").greaterThanEquals(now).get());
document.putAll(qb.get());
document.put("status", "running");
DBCursor cursor = getDbCollection().find(document).sort(new BasicDBObject("reported_time", 1));
while(cursor.hasNext()) {
list.add((Map<String, Object>) cursor.next().toMap().get("message"));
}
return list;
}
- QueryBuilder qb = new QueryBuilder(), instantiates a new QueryBuilder.
- The logic build by the QueryBuilder in the example above is; (starting date = null and ending date = null) or (starting date >= now and ending date >= now) . [qb.or(new QueryBuilder().put("starting_date").is(null).put("ending_date").is(null).get(),
new QueryBuilder().put("starting_date").lessThanEquals(now).put("ending_date").greaterThanEquals(now).get());] - document.putAll(qb.get()) adds the logic constructed to the DBObject.
thanks,
Shyarmal.
No comments:
Post a Comment