You have an HTML form that allows your customer to add an arbitrary amount of stuff to something. stuff is a partial JSON document which is a list of String/String and String/Date NVPs contained within something. Mongo will by default store the date as an ISODate. With that said, the something collection looks something like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.something.find().forEach(printjson) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_id": "4f0c56f1b8eea0b686189c90", | |
"meh": "meh", | |
"feh": "feh", | |
"arrayOfStuff": [ | |
{ | |
"name": "Elgin City", | |
"date": "2012-01-06T14:54:21.000Z" | |
}, | |
{ | |
"name": "Rangers", | |
"date": "2012-02-02T11:01:27.000Z" | |
}, | |
{ | |
"name": "Arsenal", | |
"date": "2012-02-03T10:56:23.000Z" | |
} | |
] | |
} | |
{ | |
"_id": "4f0c56f1b8eea0b686189c99", | |
"meh": "meh meh meh meh", | |
"feh": "feh feh feh feh feh feh", | |
"arrayOfStuff": [ | |
{ | |
"name": "Satriani", | |
"date": "2011-11-01T11:51:46.000Z" | |
}, | |
{ | |
"name": "Vai", | |
"date": "2012-01-01T15:16:21.000Z" | |
}, | |
{ | |
"name": "Johnson", | |
"date": "2012-03-01T12:11:27.000Z" | |
} | |
] | |
} |
The simple solution to the format issue is to display the Date as an ISODate on the form, thus when the form is submitted, the date can be treated just like any other string (though you still need to tell mongo to treat it as a Date and not a String, so you need to 'find' it and change the type; more on that later).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new Date(stuff.date.$date).toISOString() |
But that would be way too easy; in the real world, easy is considered as rare as unicorn herders. The Use Case/User Story/Whatever acceptance criteria is that we display the Date in the full UTC form. OK then.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new Date(stuff.date.$date) |
This is easily done, in fact, it is easier than displaying it as an ISODate, but now we have to parse it and convert to an ISODate, which involves finding it in the JSON document first.
Parsing a UTC date into an ISO is trivial but should have been easier than this - I needed to faff around with the DateFormat string before I got rid of the ParseExceptions.
This is roughly how I did it. Doesn't look too pretty tbh.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DBObject dbo = DBObjectFactory.fromJson(json); | |
DBObject set = new BasicDBObject(); | |
Set<String> keys = dbo.keySet(); | |
for (Object key : keys) { | |
if("arrayOfStuff".equals((String)key)) { | |
DBObject arrayOfStuff = (BasicDBList)dbo.get("arrayOfStuff"); | |
BasicDBList newArrayOfStuff = new BasicDBList(); | |
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'zzz"); | |
for(Object stuff : arrayOfStuff.keySet()) { | |
DBObject nvps = (DBObject)arrayOfStuff.get((String)stuff); | |
DBObject newStuff = new BasicDBObject(); | |
newStuff.put("name", nvps.get("name")); | |
newStuff.put("date", df.parse((String)nvps.get("date"))); | |
newArrayOfStuff.add(newStuff); | |
} | |
set.put("arrayOfStuff", newArrayOfStuff); | |
} | |
DBObject update = new BasicDBObject(); | |
update.put("$set", set); |
It's amazing how simple this should be. You set it up as a date, you pass in a date, yet you still need to explicitly convert it from initially from a String to Date, and then from UTC to ISO.
below js code outputs 'Tue, 21 May 2013 06:57:39 GMT'
ReplyDeletevar now = new Date();
console.log(new.toUTCString());
How can i remove GMT in using java script? Is there any UTC js methods avialble for this?
Thanks
Grep Command Examples in Linux/Unix
This is pretty simple...
ReplyDeletevar string = string.replace(' GMT', '');