title: Morphia MongoDB check for null and non existing field
body: <p>I am new to both Morphia and MongoDB. Is there a way to check using Morphia that a certain field in my database is not <code>null</code> and also exists. For example from the following record of a user from a collection of users in database:</p>
  
  <pre><code>{ "_id" : ObjectId("51398e6e30044a944cc23e2e"), 
    "age" : 21 , 
    "createdDate" : ISODate("2013-03-08T07:08:30.168Z"),
    "name" : "Some name" }
  </code></pre>
  
  <p>How would I use a Morphia query to check if field <code>"createdDate"</code> is not null and exists.</p>
  
  <p><strong>EDIT:</strong>
  I am looking for a solution in Morphia. So far I have come up with this:</p>
  
  <pre><code>query.and(query.criteria("createdDate").exists(),
  query.criteria("createdDate").notEqual(null));
  </code></pre>
  
  <p>From documentation, I learnt Morphia does not store empty or null fields. Hence the justification for <code>notEqual(null)</code>.</p>
  
  <p><strong>EDIT 2:</strong> From the answers I can see the problem needs more explanation. I cannot modify the <code>createdDate</code>. To elaborate: the example above is less complex than my actual problem. My real problem has sensitive fields which I cannot modify. Also to complicate things a bit more, I do not have control over the model otherwise I could have used <code>@PrePersist</code> as proposed in one of the answers.</p>
  
  <p>Is there a way to check for null and non existing field when I have no control over the model and I am not allowed to modify fields?</p>