title: What is needed in order to query through mongoDB collection?
body: <p>I am running mongoDB on Ubuntu. I have a database called 'temporary'. Inside that database I have a collection called 'questions'.</p>
  
  <p>Using mongo on the command line I was able to successfully insert objects into the 'questions collection'. So far so good. My next step was to write a program in JavaScript that would run and insert some more objects into the collection. That worked fine, I was able to connect to my collection and add more to it.</p>
  
  <p>Now here's where I'm having problems. In a JavaScript program, I would like to make queries and retrieve certain data from my 'questions' collection. My code looks like this:</p>
  
  <pre><code>var mc = require('mongodb').MongoClient;
  //connect to database
  db = mc.connect('mongodb://localhost/temporary', function(err,db){
      var myCollection = db.collection('questions');
      var newArray = myCollection.find().toArray();
      db.close();
  }
  </code></pre>
  
  <p>I believe I am connecting to my collection correctly because that's how I connected to it to add entries. My problem is with the find() method and the toArray() method. </p>
  
  <p>From my understanding find() let's me search through my collection and ends up returning something called a cursor. I want to take the objects and put them into an array using toArray(). </p>
  
  <p>The problem is this line gave me an error:</p>
  
  <pre><code>var newArray = myCollection.find().toArray();
  </code></pre>
  
  <p>The error says "Error: Callback is mandatory". So I ended up searching and found this website: <a href="http://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html" rel="nofollow noreferrer">http://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html</a></p>
  
  <p>From there I decided to try:</p>
  
  <pre><code> var newArray = myCollection.find().toArray(function(err, items){
       if(err){ console.log("An error occured");}});
  </code></pre>
  
  <p>With this, I keep getting "An error occured", printing out. Does anyone have any idea what the issue could be? It's obviously with the toArray() method but I don't know what the cause for the error is. I feel like my code is pretty much just the bare bones right now and I can't for the life of me figure out what I'm doing wrong.</p>
  
  <p>To clarify, I get no error when I just do the find() method by itself but for some reason adding .toArray() breaks my code. I thought perhaps it's because my  my find() call has parameters  but I get the same error even when searching for objects that I know are in the collection, so it's not that.</p>
  
  <p>I'm connecting to my database. I create a collection 'questions' that already should exist and have objects that were inserted into it. I use find() to do a query, try retrieve objects into an array, and then I want to close the database.</p>