Is there a way to Insert an ISODate type in a BSONWritable Class to produce a MongoDB document that includes an ISODate() value

2,685 views
Skip to first unread message

Robert

unread,
May 11, 2012, 6:08:01 PM5/11/12
to mongod...@googlegroups.com

MongoDb recognizes the several data types, including ISODate. I'm developing MapReduce utilities in which the expect MongoDb format output is

{ "_id" : "2012-02-10-Report", "count" : 4, "_type" : "Report", "date" : ISODate("2012-02-10T11.39.05Z"), "ts" : 1328873945000 }


However, the best I can do, so far is to produce it as follows to enclose the ISODate type in quotes and the date in single quotes:

{ "_id" : "2012-02-10-Report", "count" : 4, "_type" : "Report", "date" : "ISODate('2012-02-10T11.39.05Z')", "ts" : 1328873945000 }


My current Mapper includes the following

SimpleDateFormat inSFM = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat isoSFM = new SimpleDateFormat("yyyy-MM-dd'T'HH.mm.ss'Z'");

BSONWritable mapOutValue = new BSONWritable();

BSONWritable acmDoc = new BSONWritable();
if (value.containsField(Consts.FIELD_PUBLISHED_AT)) {
  pubAt = value.get(Consts.FIELD_PUBLISHED_AT).toString();
}

try {
  pubDate = inSFM.parse(pubAt);
} catch (ParseException e) {

...
}
isoSFM.setTimeZone(TimeZone.getTimeZone("UTC"));

String isoDate = "ISODate('" + isoSFM.format(pubDate)
.toString() + "')";

mapOutValue.put("date", isoDate);

...

context.write(mapOutKey, mapOutValue);


Subsequent code, is processed by a Reducer<Text, BSONWritable, Text, BSONWritable> class. This is quite elegantly done in JavaScript. Yet, I have not found an equivalent approach to save types in Java except longs which convert to NumberLong("<a long number>")

Example: mapOutValue.put("mylong", longTypeValue);

Is there a way to use the BSONWritable in and other MongoDB utilities so and ISODate("<ISODatestring>") is the final output in a JSON string for insert to MongoDB?

I thought I could use mapOutValue.put("date", date);, but that just produces a date string.

Robert

unread,
May 15, 2012, 6:41:36 AM5/15/12
to mongod...@googlegroups.com
This issue may be related to parsing ISODate into a MongoDB BasicDBObject:

Robert

unread,
May 17, 2012, 6:15:16 AM5/17/12
to mongod...@googlegroups.com
The ISODate issue is resolved by upgrading MongoDB from version 1.6 to 2.0. The earlier version produced the date based on locality and in string format. The later version produces the date in ISODate() format with the date presented in UTC ISO format.


On Friday, May 11, 2012 6:08:01 PM UTC-4, Robert wrote:

Sam Millman

unread,
May 17, 2012, 6:23:38 AM5/17/12
to mongod...@googlegroups.com
Yes the date object was changed from Date() to ISODate() in v1.6 if I remember right.

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to

Robert

unread,
May 17, 2012, 6:34:07 AM5/17/12
to mongod...@googlegroups.com
I could only produce a date string with v1.6.3.

On Thursday, May 17, 2012 6:23:38 AM UTC-4, Sammaye wrote:
Yes the date object was changed from Date() to ISODate() in v1.6 if I remember right.

Sam Millman

unread,
May 17, 2012, 6:37:07 AM5/17/12
to mongod...@googlegroups.com
Form the shell? Or did you only try in the Java driver? Cos it is possible that is a quirk in the Java Driver when using Legacy objects

Robert

unread,
May 17, 2012, 8:05:04 AM5/17/12
to mongod...@googlegroups.com
Good point. My Java driver failed to produce it.

 
On Thursday, May 17, 2012 6:37:07 AM UTC-4, Sammaye wrote:
Form the shell? Or did you only try in the Java driver? Cos it is possible that is a quirk in the Java Driver when using Legacy objects

Reply all
Reply to author
Forward
0 new messages