title: Can I user fasterxml to write a date as ISODate to use an expiring mongodb index?
body: <p>I am using <a href="http://wiki.fasterxml.com/" rel="nofollow">FasterXML</a> to map my objects to MongoDB</p>
  
  <p>I'd like to use an <a href="http://docs.mongodb.org/manual/tutorial/expire-data/#expire-data-from-collections-by-setting-ttl" rel="nofollow">expiring index</a>, but for that, I need an ISODate field on my document.</p>
  
  <p>If my java class has a Date field, it gets serialised either by a number or a string, using the <code>DateSerializer</code> as described here: <a href="http://wiki.fasterxml.com/JacksonFAQDateHandling" rel="nofollow">http://wiki.fasterxml.com/JacksonFAQDateHandling</a></p>
  
  <p>I tracked it down to this function:</p>
  
  <pre><code>/**
   * Method that will handle serialization of Date(-like) values, using
   * {@link SerializationConfig} settings to determine expected serialization
   * behavior.
   * Note: date here means "full" date, that is, date AND time, as per
   * Java convention (and not date-only values like in SQL)
   */
  public final void defaultSerializeDateValue(Date date, JsonGenerator jgen)
      throws IOException, JsonProcessingException
  {
      // [JACKSON-87]: Support both numeric timestamps and textual
      if (isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
          jgen.writeNumber(date.getTime());
      } else {
          jgen.writeString(_dateFormat().format(date));
      }
  }
  </code></pre>
  
  <p>None of those two paths ends up writing a standard mongodb date type, and thus my index does not work.</p>
  
  <p>Is there a way to force the java <code>Date</code> type to be serialised as it would be when creating the document from the mongo shell?  Alternatively, can I automatically add the field via a "trigger" or something like that? (with the objective of bypassing the serializer altogether)</p>