title: MongoDB - Negate $elemMatch query
body: <p>Having this collection of documents called accommodations...</p>
  
  <pre><code>[{
      name: String,
      bookings: [{
          from: Date
          to: Date
          status: String //'CONFIRMED', 'ON_REQUEST', 'PAID'
      }]
  }]
  </code></pre>
  
  <p>I want to find all accommodations that doesn't have any booking between the giving dates with the exception of those bookings that have 'ON_REQUEST'.</p>
  
  <pre><code>var searchFrom = new Date(2015, 02, 02);
  var searchTo = new Date(2015, 02, 05);
  
  {
      name: 'My awesome accommodation',
      bookings: [
      {
        from: Date(2015, 02, 01),
        to: Date(2015, 02, 03),
        status: 'CONFIRMED',
      }, {
        from: Date(2015, 02, 04),
        to: Date(2015, 02, 07),
        status: 'ON_REQUEST'
      }, {
        from: Date(2015, 02, 08),
        to: Date(2015, 02, 10),
        status: 'PAID'
      }]
  }
  </code></pre>
  
  <p>I'm trying this but $elemMatch condition doesn't discard not allowed bookings and if i negate the $elemMatch it keeps giving me results because both $elemMatch will always match results.</p>
  
  <pre><code>db.accommodations.find({
      'bookings': {
          $and: [{
              $elemMatch: {
                  'from': {$lte: searchTo},
                  'to': {$gte: searchFrom},
                  'status': 'ON_REQUEST'
              }
          }, {
              $not: {
                  $elemMatch: {
                      'from': {$lte: searchTo},
                      'to': {$gte: searchFrom},
                      'status': {$in: ['CONFIRMED', 'PAID']
                  }
              }
          }]
      }
  });
  </code></pre>
  
  <p>I wonder how can i solve this with one query and avoid to dev some logic before the query.</p>
  
  <p>Edit: @karthick.k</p>
  
  <p>In this case, the expected result should be empty, because there are bookings in the giving dates, so, i shouldnt be able to book.</p>
  
  <p>If the only existing booking were the 'ON_REQUEST' booking, the expected result should be that accommodation. This means that in the booking dates the user should be able to ask for booking the accommodation (probably under some conditions that both parts should agree).</p>