MongoDB Java driver edit a cursor after it has been retrieved -
in mongodb java driver retrieve documents query.
dbcursor cursor = dbcollection.find(query).sort(new basicdbobject("date", -1)); return json.serialize(cursor); this works fine returns following:
{ "issuccessful": true, "result": [ { "date": { "$date": "2014-11-26t23:00:00.000z" }, value: 20 } ] } but: want edit 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 while loop doesn't affect returned result.
gives same result back. how change date field , return it? i've put following line:
simpledate.format(dbo.get("date")) in system.out.printline("");
, prints out "27-11-2014", want it.
i solved doing following:
dbcursor cursor = dbcollection.find(query).sort(new basicdbobject("date", -1)); list<dbobject> arr = new arraylist<dbobject>(); while(cursor.hasnext()){ dbobject dbo = cursor.next(); dbo.put("date", simpledate.format(dbo.get("date"))); arr.add(dbo); } return json.serialize(arr); summary: pass every dbobject cursor while loop, here edit simpledateformat
simpledate simpledateformat("dd-mm-yyyy") object then, put every item in arraylist, serialize json.serialize, return javascript.
Comments
Post a Comment