title: Random Records from MongoDB
body: <p>this is my first time asking a question on StackOverflow, and I apologize for it being more or less a repeat or if I've posted anything incorrectly. I'm trying to follow the code in <a href="https://stackoverflow.com/questions/2824157/random-record-from-mongodb">Random record from MongoDB</a>, but still struggling to retrieve 4 random records from my MongoDB in Node. </p>
  
  <p>In the Mongo shell, I can successfully retrieve 4 random records with this: </p>
  
  <pre><code>db.reccs.find( { genre: { $in: ['fantasy']} } ).limit(4).skip(Math.random() *   db.reccs.count( { genre: { $in: req.query.test} } ))
  </code></pre>
  
  <p>However, in my Node.js server.js file, this code returns no errors and the first 4 records it finds, not a random 4: </p>
  
  <pre><code>  app.get('/comics', function(req, res) {
    var comics = db.collection('reccs').find( { genre: { $in: req.query.test} } ).limit(4).skip(Math.random() * db.collection('reccs').count( { genre: { $in: req.query.test} } )).toArray(function(error, comics) {
    if (error) {console.dir(error+"error!")}
  
    res.send(comics)
  }) })
  </code></pre>
  
  <p>I'm totally stumped, so I'd appreciate any thoughts on why this works in the Mongo shell but not in server.js. Thank you!</p>
  
  <p>edit - I believe this question is different because it is not about the Mongo shell, but Node.js - the solution in the linked question does absolutely work in the shell, just not in Node.js.</p>
  
  <p>edit 2 - If anyone ever comes across this, this <a href="https://stackoverflow.com/questions/13148875/mongodb-count-undefined">MongoDB count() undefined</a> along with the comment below should help!</p>