title: ETag facility in MongoDB document - Java driver
body: <p>My Java application has REST GET apis (implemented with JAX-RS) that access MongoDB collection.
  I'm trying to <strong>introduce web cache</strong> to reduce response time (by reducing document read at MongoDB side). </p>
  
  <p>I'm looking for standard way to provide <strong>ETag</strong> (<a href="http://en.wikipedia.org/wiki/HTTP_ETag" rel="nofollow">http://en.wikipedia.org/wiki/HTTP_ETag</a>) for resources(resources are derived from documents in MongoDB collection), so that <strong>ETag in MongoDB collection</strong> will be updated automatically every time document is updated.</p>
  
  <p>Now, I can validate ETag in HTTP request ("If-None-Match") with ETag in MongoDB document, and if both are same return 304 (Not Modified). Now, WebCache can return already cached resource to client, thus improve response time.</p>
  
  <p>While googling, I got following 2 options :</p>
  
  <ol>
  <li><p>hascode() as ETag : get document from MongoDB and populate Java
  object, then, calculate hashCode() which is used as ETag.  But I
      want to avoid this overhead (full doc read + hashcode calculation)
      <a href="https://devcenter.heroku.com/articles/jax-rs-http-caching" rel="nofollow">https://devcenter.heroku.com/articles/jax-rs-http-caching</a></p></li>
  <li><p>Last modified date as ETag: Add new field "Last modified" to mongoDB
  collection.
      <a href="http://howtodoinjava.com/2013/06/05/jax-rs-resteasy-cache-control-with-etag-example/" rel="nofollow">http://howtodoinjava.com/2013/06/05/jax-rs-resteasy-cache-control-with-etag-example/</a>
      This mechanism is more suitable for "Last Modified" header (HTTP
      response).  It looks, date is misused here for ETag. Here I've to
      modify existing doc to insert new field (updatedTime).  Again,
      precision of HTML date is sec and mongoDB date is msec.</p></li>
  </ol>
  
  <p>Please sugggest standard way to provide ETag in MongoDB.</p>