title: MongoDB/Java: Type safety in DBObjects
body: <p>I have a field in a MongoDB document that stores an arbitrarily-large number.  When I retrieve it as a DBObject (Java driver for MongoDB), I sometimes run into a ClassCastException:</p>
  
  <pre><code>DBObject obj = collection.findOne();
  long val = (Long)(o.get("numericVal"));
  </code></pre>
  
  <p>If the value stored in <code>numericVal</code> is, say, 1234567890, the cast to Long succeeds.  If it is, say, 12345, DBObject.get() returns a Double, and the cast fails.</p>
  
  <p>How can I ensure type safety when deserializing MongoDB DBObjects?</p>