Sunday, 14 May 2017

Unable to convert MongoDB collection to list

I'm trying to iterate a MongoDB collection and convert it into a list. But the array list is always empty. Here is the code:

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

getPost: function(callback){
    MongoClient.connect(url, function(err, db){
        var cursor = db.collection('post').find();
        var list = [];
        cursor.forEach(function(doc){
            list.push(doc);
        });
        callback(list);
    })
}

list is always [].



via iJade

No comments:

Post a Comment