In my MongoDB Java driver I retrieve some documents with a query.
DBCursor cursor = dbCollection.find(query).sort(new BasicDBObject("date", -1));
return JSON.serialize(cursor);
This works fine it returns the following:
{
"isSuccessful": true,
"result": [
{
"date": {
"$date": "2014-11-26T23:00:00.000Z"
},
value: 20
}
]
}
But: I want to edit the field $date using
SimpleDateFormat
I've tried this:
DBCursor cursor = dbCollection.find(query).sort(new BasicDBObject("date", -1));
while(cursor.hasNext()){
DBObject dbo = cursor.next();
dbo.put("date", simpleDate.format(dbo.get("date")));
}
return JSON.serialize(cursor);
But the while loop doesn't affect the returned result.
It just gives the same result back. How do I change the date field and then return it?
Also i've put the following line:
simpleDate.format(dbo.get("date"))
In a System.out.printline("");
And this prints out "27-11-2014", just like i want it.