title: MongoDb and NodeJs // Store data from DB to variable
body: <p>UPDATE: After first answerI realized that should clarify. I need to retrieve data from 2 different documents from db and compare them. So I was thinking about solution and seems to find it.</p>
  
  <p><em>My question was:</em></p>
  
  <p>I'm new in NodeJs and MongoDb and I'm stuck trying to set data received from mongoDb to variable:</p>
  
  <pre><code>let variableToStoreData1;
  let variableToStoreData2;
  
  db.collection('costs').find({}).toArray(function(err, result) {
     console.log('result', result[0].user.last_name);
     variableToStoreData1 = result;
  });
  
  db.collection('messages').find({}).toArray(function(err, result) {
     console.log('result', result[0].user.last_name);
     variableToStoreData2 = result;
  });
  
  console.log(variableToStoreData1) // undefined
  console.log(variableToStoreData2) // undefined
  </code></pre>
  
  <p><em>My solution</em>
      let variableToStoreData1;
      let variableToStoreData2;</p>
  
  <pre><code>db.collection('costs').find({}).toArray(function(err, result) {
    console.log('result', result[0].user.last_name);
    variableToStoreData1 = result;
  });
  
  db.collection('messages').find({}).toArray(function(err, result) {
   console.log('result', result[0].user.last_name);
   variableToStoreData2 = result;
   callback();
  });
  
  function callback () {
    console.log(variableToStoreData1) // now it's defined
    console.log(variableToStoreData2) // now it's defined
  }
  </code></pre>