query.put("publishedTimestamp", new BasicDBObject("$gt", afterTimestamp));
BasicDBObject byTimestamp = new BasicDBObject();
byTimestamp.put("publishedTimestamp", 1);
DBCursor cursor = collection.find(query).sort(byTimestamp);
List<Message> docs = new ArrayList<Message>();
long lastTimestamp = Long.MIN_VALUE;
while (cursor.hasNext())
{
DBObject next = cursor.next();
Message message = deserialise(next, Message.class, true);
if (docs.size() >= preferredLimit &&
lastTimestamp != message.getPublishedTimestamp())
{