title: c# - How to use DateTimeOffset in MongoDB
body: <pre><code>public class ScheduledEvent : Event
  {
      public DateTimeOffset StartDateTime { get; set; }
  }
  </code></pre>
  <p>StartDateTime = 5/27/2013 2:09:00 AM +00:00 representing 05/26/2013 07:09 PM PST</p>
  <p>What's recorded in MongoDB:</p>
  <pre><code>db.ScheduledEvent.find().toArray()[
      {
          &quot;_id&quot;: BinData(3, &quot;ZE2p31dh00qb6kglsgHgAA==&quot;),
          &quot;Title&quot;: &quot;Today 26th at 7:09pm&quot;,
          &quot;Length&quot;: &quot;00:00:00&quot;,
          &quot;MoreInformation&quot;: &quot;http://1.com&quot;,
          &quot;Speakers&quot;: [{
              &quot;_id&quot;: BinData(3, &quot;ndzESsQGukmYGmMgKK0EqQ==&quot;),
              &quot;Name&quot;: &quot;Mathias Brandewinder&quot;
              }
          ],
          &quot;Location&quot;: {
              &quot;_id&quot;: BinData(3, &quot;AAAAAAAAAAAAAAAAAAAAAA==&quot;),
              &quot;Name&quot;: &quot;Somwhere &quot;
          },
          &quot;Organizers&quot;: [{
              &quot;_id&quot;: BinData(3, &quot;AAAAAAAAAAAAAAAAAAAAAA==&quot;),
              &quot;Name&quot;: null
          }],
          &quot;CreatedOn&quot;: [
              NumberLong(&quot;635052144104050898&quot;),
              0
          ],
          &quot;StartDateTime&quot;: [
              NumberLong(&quot;635052173400000000&quot;),
              0
          ]
      }
  ]
  </code></pre>
  <hr />
  <p>I realize that StartDateTime is stored as Ticks in MongoDB.</p>
  <pre><code>var dateMarker = DateTimeOffset.UtcNow;
  var nextDay = dateMarker.AddDays(1);
  </code></pre>
  <p>This query does not work:</p>
  <pre><code>var today = EventRepoistory
      .All()
      .Where(z =&gt; z.StartDateTime &gt;= dateMarker 
          &amp;&amp; z.StartDateTime &lt; nextDay)
      .OrderByDescending(z =&gt; z.StartDateTime)
      .ToList();
  </code></pre>
  <p>I have added a query display to the Mongo C# driver which shows the following query:</p>
  <pre><code>{ &quot;$query&quot; : { &quot;StartDateTime&quot; : { &quot;$gte&quot; : [NumberLong(&quot;635052168609734070&quot;), 0], &quot;$lt&quot; : [NumberLong(&quot;635053032609734070&quot;), 0] } }, &quot;$orderby&quot; : { &quot;StartDateTime&quot; : -1 } }
  </code></pre>
  <blockquote>
  <p><code>Lower bound = 6350521 68609734070</code></p>
  </blockquote>
  <blockquote>
  <p><code>Server      = 6350521 73400000000</code></p>
  </blockquote>
  <blockquote>
  <p><code>Upper bound = 6350530 32609734070</code></p>
  </blockquote>
  <p><strong>Question: Why this MongoDB query doesn't return anything?</strong></p>
  <pre><code>db.ScheduledEvent.find({
    &quot;$query&quot;: {
      &quot;StartDateTime&quot;: {
        &quot;$gte&quot;: [NumberLong(&quot;635052168609734070&quot;), 0],
        &quot;$lt&quot;: [NumberLong(&quot;635053032609734070&quot;), 0]
      }
    },
    &quot;$orderby&quot;: {
      &quot;StartDateTime&quot;: -1
    }
  })
  </code></pre>
  <p>I have researched about this topic and I found this solution :
  <a href="https://stackoverflow.com/questions/10480127/mongodb-and-datetimeoffset-type">MongoDB and DateTimeOffset type</a></p>
  <p>But it seams that LINQ provider is doing what it's supposed to?</p>
  <hr />
  <p>I have also tried code:</p>
  <pre><code>db.ScheduledEvent.find({
    &quot;StartDateTime&quot;: {
      &quot;$gte&quot;: [NumberLong(&quot;1&quot;), 0]
    }
  })
  </code></pre>
  <p>But it doesn't return any result.</p>