0

Is there a way to create a synchronous find() for a mongo query?

Like:

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/mydb', function(err, db){

  if(err) throw err;

  var collection = db.collection('mycollection');

  var myarray  = collection.find({});

  //use myarray
  for (var i = myarray.length - 1; i >= 0; i--) {
    console.log(myarray[i]);
  };

});
SOUser
  • 610
  • 1
  • 11
  • 25
  • What exactly do you want? – Salvador Dali Nov 20 '13 at 22:46
  • If you're wanting to do this in NodeJS, you should probably add it's tag. – Corey Ogburn Nov 20 '13 at 22:47
  • 1
    No, there is not really a good way. Embrace the asynchronous behavior. Use Promises or a good async library if you want to maintain some sanity when dealing with complex async flows. Also, this has been asked before: http://stackoverflow.com/questions/12030248/what-is-the-right-way-to-make-a-synchronous-mongodb-query-in-node-js/16982359#16982359 – WiredPrairie Nov 21 '13 at 01:13

0 Answers0