title: mongo BadValue unknown operator: $or
body: <p>The collection has one document:</p>
  
  <pre><code>{
      "_id" : ObjectId("54b513933aca242d9915a787"),
      "carriers" : [ 
          {
              "carrier" : ObjectId("54b54d223aca242d9915a788"),
              "carryingInterval" : {
                  "from" : ISODate("2013-12-31T23:00:00.000Z"),
                  "to" : null
              }
          }
      ]
  }
  </code></pre>
  
  <p>I would like to make a criteria, that is looking for a certain carrier and range.
  The <strong>carryingInterval</strong>'s <strong>from</strong> and <strong>to</strong> fields could be a <strong>Date</strong> object or <strong>null</strong>. The <strong>null</strong> value means +/- infinity.</p>
  
  <p>My criteria is:</p>
  
  <pre><code>{
      carriers: {
          $elemMatch: {
              carrier: ObjectId('54b54d223aca242d9915a788'),
              carryingInterval: {
                  $or: [
                      {
                          from: {$lt: Date()},
                          to: null
                      }
                  ]
              }
          }
      }
  }
  </code></pre>
  
  <p>This query cannot run with the following error message:</p>
  
  <blockquote>
    <p><em>Can't canonicalize query: BadValue unknown operator: $or</em></p>
  </blockquote>
  
  <p>Okey, I tried other criterias, like this:</p>
  
  <pre><code>{
      carriers: {
          $elemMatch: {
              carrier: ObjectId('54b54d223aca242d9915a788'),
              $or: [
                  {
                      "carryingInterval.from": {$lt: Date()},
                      "carryingInterval.to": null
                  }
              ]
          }
      }
  }
  </code></pre>
  
  <p>It runs without any result however the <strong>from</strong> field is less then 'now' in the document. If I skip the <strong>from</strong> field from the criteria, i get it.</p>
  
  <p>In this <a href="https://groups.google.com/forum/#!topic/mongodb-user/hNgue4NZqtI" rel="nofollow">topic</a> Will Berkeley says</p>
  
  <blockquote>
    <p>$or should be on the top level</p>
  </blockquote>
  
  <p>but in my second criteria the <strong>$or</strong> operator isn't on the top level, that is below the <strong>$elemMatch</strong> operator, and it works.</p>
  
  <p>I don't understand this behavior. Please, explain me! Thx! :)</p>