title: Check if document exists in mongodb using es7 async/await
body: <p>I'm trying to check if the user with the <code>email</code> provided exists in the collection <code>users</code>, but my function keeps returning undefined for every call. I use es6 and <code>async/await</code> in order to get rid of lots of callbacks. Here's my function (it is inside of a class):</p>
  
  <pre><code>async userExistsInDB(email) {
      let userExists;
      await MongoClient.connect('mongodb://127.0.0.1:27017/notificator', (err, db) =&gt; {
          if (err) throw err;
  
          let collection = db.collection('users');
  
          userExists = collection.find({email: email}).count() &gt; 0;
          console.log(userExists);
  
          db.close();
      });
      console.log(userExists);
      return userExists;
  }
  </code></pre>
  
  <p>So, the first <code>console.log</code> inside the <code>.connect</code> call always returns <code>false</code> because the returned value of the <code>.find</code> is not an array, it is some huge object which looks like this:</p>
  
  <pre><code>{ connection: null,
    server: null,
    disconnectHandler: 
     { s: { storedOps: [], storeOptions: [Object], topology: [Object] },
       length: [Getter] },
    bson: {},
    ns: 'notificator.users',
    cmd: 
     { find: 'notificator.users',
       limit: 0,
       skip: 0,
       query: { email: 'email@example.com' },
       slaveOk: true,
       readPreference: { preference: 'primary', tags: undefined, options: undefined } },
    options: 
  ........
  ........
  </code></pre>
  
  <p>And the last <code>console.log</code> is always undefined (although I think it shouldn't be like that, because <code>await</code> waits for the end of the asynchronous call, right?)</p>
  
  <hr>
  
  <p>I just need my function to return a boolean value, not a <code>Promise</code> or something.</p>
  
  <p>Can anybody help me with that?</p>
  
  <h2>UPDATE 1</h2>
  
  <p><code>console.log(collection.findOne({email: email}));</code> inside the <code>.connect</code> returns this:</p>
  
  <pre><code> { 'Symbol(record)_3.ugi5lye6fvq5b3xr': 
     { p: [Circular],
       c: [],
       a: undefined,
       s: 0,
       d: false,
       v: undefined,
       h: false,
       n: false } }
  </code></pre>
  
  <hr>
  
  <h2>UPDATE 2</h2>
  
  <p>Seems like it was the issue with my poor knowledge of es7 <code>async/await</code>.</p>
  
  <p>Now the code inside the <code>.connect</code> returns the needed value.</p>
  
  <pre><code>async userExistsInDB(email) {
      let userExists;
      await* MongoClient.connect('mongodb://127.0.0.1:27017/notificator', async(err, db) =&gt; {
          if (err) throw err;
  
          let collection = db.collection('users');
          userExists = await collection.find({email: email}).limit(1).count() &gt; 0;
  
          db.close();
      });
      console.log(userExists); // &lt;--- this is not called at all
      return userExists;
  }
  </code></pre>
  
  <p>However, now the <code>console.log</code> or anything after the <code>.connect</code> call is not performed at all.</p>
  
  <p>Now, every time I call the <code>userExistsInDB()</code> function somewhere and <code>console.log</code> its result, I get this:</p>
  
  <pre><code> { 'Symbol(record)_3.78lmjnx8e3766r': 
     { p: [Circular],
       c: [],
       a: undefined,
       s: 0,
       d: false,
       v: undefined,
       h: false,
       n: false } }
  </code></pre>
  
  <p>Any ideas why it is like that?</p>