title: How can I execute JavaScript on the server when using the Java driver
body: <p>I am using MongoDB with Clojure and Congomongo and I am trying to execute some basic Java-Script while inserting documents.</p>
  
  <p>There are two use cases were I want to execute some Java-Script during the an insert:
  a) writing a last-modifed Timestamp 
  b) creating a version tag as ObjectId</p>
  
  <p>Both are very similar, here are some MongoDB Shell examples.</p>
  
  <pre><code>db.test.update({_id:ObjectId("4fe3304fc2e61906ccdd7364")}, {$set: {created:Date()}}, false, false)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>db.test.insert({version:ObjectId(), foo:"Bar"})
  </code></pre>
  
  <p>Does anyone have an idea how I can do it using Congomongo or the plain Java-Driver?</p>
  
  <p>I tried </p>
  
  <pre><code>org.bson.types.Code
  org.bson.types.CodeWScope
  </code></pre>
  
  <p>and got something like:</p>
  
  <pre><code>{ "_id" : ObjectId("4fe32998c2e61906ccdd735f"), "version" : function cf__14_anon() { return ObjectId(); } } 
  </code></pre>
  
  <p>which is interesting but not helpful. Unfortunately it's not possible to create the timestamps/version/ObjectId on the client, because I can't make sure that the clocks of the clients are synchronous. We use the <code>version</code> synchronize/replicate data between server and the clients and creating versions/timestamps in the past does jeopardize this process.</p>
  
  <p>Thanks for your help in advance ....</p>