title: Pymongo: Aggregate all documents that not have one field and have another - group by
body: <p>I'm trying to get a set of documents from my collection with the following conditions:</p>
  
  <ul>
  <li>field: img_status doesn't exist</li>
  <li>field: images should exist</li>
  </ul>
  
  <p>And then, group those documents (unique/distinct) by one field.
  When I execute the query in MongoDB, it seems as if it returns the correct values:</p>
  
  <pre><code>db.getCollection('products').aggregate([
  { $match: { images: { $exists: true, $ne: null } } },
  { $match: { img_status: { $exists: false } } },
  { $group : { _id:"$vendor_link", "uuid" : {$first: "$uuid"}, "images": { $first: "$images"} } }
  ])
  </code></pre>
  
  <p>But in PyMongo, I always get the opposite like: <code>img_status exist = true</code>:</p>
  
  <pre><code>pipeline = [
      {
          "$match": 
          {   
              "images" : 
              { "$ne" : "null", "$exists": "true", } 
          }
      },
      {
          "$match": 
          {   
              "img_status": 
              {"$exists": "false"}
          }
      },
      {
          "$group":
          {
              "_id"       : "$vendor_link",
              "images"    : {"$first": "$images"},
              "uuid"      : {"$first": "$uuid"},
              "source"    : {"$first": "$source"}
          }},
  ]
  pprint(list(self.collection.aggregate(pipeline)))
  </code></pre>
  
  <p>What am I doing wrong?</p>