title: Pymongo Query Filtering a versioned data
body: <p>Here's a sample data from my database:</p>
  
  <pre><code>  {'data': 
       [{'is_active': True, 'printer_name': 'A', 'vid': 14510}, 
        {'is_active': True, 'printer_name': 'Changed A', 'vid': 14511}
       ]
    }, 
    {'data': 
      [{'is_active': False, 'printer_name': 'B', 'vid': 14512}]
    }
  </code></pre>
  
  <p>The <code>vid</code> field here is the <code>version id</code>. Whenever a record is edited, the modified data is pushed into the list and it therefore has a higher vid than its old version.</p>
  
  <p>Now I want to define a method called <code>get_all_active_printers</code> which returns all printers with <code>is_active :True</code></p>
  
  <p>Here's my attempt, but it returns both printers when it should not return printer <code>B</code>  </p>
  
  <pre><code>def get_all_active_printers():
      return printers.find(
          {'data.is_active':  True}, {"data": {"$slice": -1}, '_id': 0, 'vid': 0})
  </code></pre>
  
  <p>Whats wrong with my query ?</p>
  
  <p><strong><em>Edit 1 in response to comment by WanBachtiar</em></strong></p>
  
  <p>Here's the actual output from using the command <code>print([c for c in get_all_active_printers()])</code></p>
  
  <pre><code>[{'data': [{'printer_name': 'Changed A', 'vid': 1451906336.6602068, 'is_active': True, 'user_id': ObjectId('566bbf0d680fdc1ac922be4c')}]}, {'data': [{'printer_name': 'B', 'vid': 1451906343.8941162, 'is_active': False, 'user_id': ObjectId('566bbf0d680fdc1ac922be4c')}]}]
  </code></pre>
  
  <p>As you can see in the actual output - the <code>is_active</code> value for Printer <code>B</code> is False, but <code>get_all_active_printers</code> still returns <code>B</code></p>
  
  <p>Here's my version details:</p>
  
  <ul>
  <li>Python 3.4.3</li>
  <li>pymongo 3.2</li>
  <li>mongodb 2.4.9</li>
  </ul>
  
  <p>On Ubuntu 14.04, if that matters.</p>
  
  <p><strong><em>Edit 2</em></strong></p>
  
  <p>Noticed yet another issue. The query is returning <code>vid</code> field, even though have clearly mentioned <code>'vid': 0</code> in the projection. </p>
  
  <p><strong>* Edit 3*</strong></p>
  
  <p>I am not sure by what you mean when you say </p>
  
  <blockquote>
    <p>"make sure that there is no other documents for {'printer_name': 'B'}"</p>
  </blockquote>
  
  <p>. Yes the second data (on printer B) has a second row. That was the first data - when the printer was created when the field <code>is_active</code> was <code>true</code>. Later it becomes <code>false</code>. Here's the snapshot of the database:</p>
  
  <p><a href="https://i.stack.imgur.com/VFnA5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VFnA5.png" alt="enter image description here"></a></p>
  
  <p>But I want to filter on the latest data as old data is only for keeping an audit trail.</p>
  
  <p>If i move <code>'data.is_active': True</code> to the projections as in the following code:</p>
  
  <pre><code>def get_all_active_printers():
      return printers.find(
          {}, {'data': {'$slice': -1}, 'data.is_active': True, '_id': 0, 'vid': 0})
  </code></pre>
  
  <p>I get the following error message:</p>
  
  <blockquote>
    <p>pymongo.errors.OperationFailure: database error: You cannot currently
    mix including and excluding fields. Contact us if this is an issue.</p>
  </blockquote>
  
  <p>So how do i filter based on the latest data, given the snapshot above ?  Sorry if my question did not make it clear earlier.</p>