Spring Data MongoDB check if field exist then query
<p>If I want to check if a date falls between a date range. I can do something like this.</p>

<pre><code>Query query = new Query();
query.addCriteria(new Criteria().andOperator(Criteria.where("effectiveFrom").lte(date),
       Criteria.where("validTill").gte(date)));
return mongoTemplate.find(query, DocumentClass.class, "DocumentName");
</code></pre>

<p>But suppose the <code>validTill</code> does exist in some cases, what is the most convenient way to do this. So it should be something like check if <code>validTill</code> exists then compare the given date within the date range otherwise just find the records where <code>effectiveFrom</code> is less than or equals to the given date.</p>