ExpressおよびMonkを使用してMongoDBアレイをループする方法は、各要素のページを作成しますか?
質問
私はExpress、Nodejs、MonkのWebアプリを構築しています。MongoDBデータベースの配列の各要素のページを作成しようとしています。
そのデータは既に collects というコレクションにあります。 coll_list のように:
{ "_id" : ObjectId("53dbaefd3d85d57492506f1f"), "coll_list" : [ "data_pagename1",
"data_pagename2", "data_pagename3" ] }
.
coll_list のすべての要素をループすることが可能であると思いました:
router.get('/index', function(req, res) {
var db = req.db;
var collection = db.get('collections');
collection.find( "coll_list" , function(e,docs) {
for (elems in docs) {
res.render(elems, {
elems : docs
});
}
});
});
.
これを行う方法についての提案やヘルプ/ポインターは大幅に感謝されます。
解決
router.get('/coll/:id',
function(req,res){
//access the id by req.params.id
//req.params.id will essentially be the _id of the document
//use it to obtain data from mongoDB and render the page using that data
//From the front end you make the call to /coll/<ObjectId> like
// /coll/53dbaefd3d85d57492506f1f and you get that id in req.params.id and use it to
//render data specific to that _id.
});
.
したがって、単一の経路を使用して、coll_list
所属していません StackOverflow