title: How does a not exists or exists query look like in Mongodb?
body: <p>I'm new to MongoDb and am trying to see if I can do an exists() query in Mongodb. </p>
  
  <p>The collection I'm playing with has users (with details such as company and a billingGroup) and expectedCompanies.</p>
  
  <p>Normally in Oracle the users and expectedCompanies would be in two tables and I would query this like this:</p>
  
  <pre><code>SELECT *
      FROM tableUsers
      WHERE exists (
          SELECT 1 
          FROM tableExpectedCompanies 
          WHERE 
              tableExpectedCompanies.companyname != tableUsers.companyName and 
              tableExpectedCompanies.billingGroup =  tableUsers.companyName.billingGroup
          )
  </code></pre>
  
  <p>In MongoDb it's now all in one collection, roughly like this: </p>
  
  <pre><code>    {
            "_id" : "billingGroupX", 
            "expectedCompanies" : ["CompanyA", "company B", "supercompany X"],
            "users" : [{"name":..., "company": ...}, {}, {}]
  }
  </code></pre>
  
  <p>I was hoping I could do something like this:</p>
  
  <pre><code>db.MasterData.find(
      {'users.company': 
          {$nin: db.MasterData.find(
                  {},
                  {'expectedCompanies:1'})
          }
      })
  </code></pre>
  
  <p>But of course failed miserably, first error was:</p>
  
  <pre><code>error: { "$err" : "$nin needs an array", "code" : 13277 }
  </code></pre>
  
  <p><strong>Is there actually an exists or not exists analogue in MongoDb ?</strong></p>
  
  <p>Or something like an <em>in</em> or <em>not in</em> condition?</p>
  
  <p><strong>edit</strong> : After initial negative feedback on my question. Please note that I'm struggling to find an exists / not exists / in / not in condition <em>where you can use a subquery</em> as it is normally done in SQL.</p>