列出MongoDB数组中的项目

时间:2013-12-31 22:29:23

标签: javascript arrays mongodb

我正在尝试列出我在文档字段“body”中存储为数组的一系列段落。我在数组中有x项,它输出整个字段x次。如果有人能指出我正确的方向,我会非常感激。我知道问题就在于我最后有#{pages.body},因为很明显这会返回整个字段,但我不知道如何更改它。

路线:

exports.cont = function(db) {
    return function(req, res) {
        var collection = db.get('contents');
            collection.find({href:req.params.href},{},function(e,docs){
                res.render('cont', {
                    "cont" : docs
                });
            });
        };
};

在Jade中显示页面:

each pages, i in cont
    .box
        h1 #{pages.title}
            .img
                img.body(src="#{pages.img}")
            .text
                each para, i in pages.body
                    p #{pages.body}

1 个答案:

答案 0 :(得分:0)

解决方案:

each pages, i in cont
    .box
        h1 #{pages.title}
            .img
                img.body(src="#{pages.img}")
            .text
                each paragraph, i in pages.body
                    p #{paragraph.text}

MongoDB文档设置:

{ 
    img: "xxxxxx",
    body:
        [
            { text: "xxxxxxx" },
            { text: "xxxxxxx" },
            { text: "xxxxxxx" }
        ]
}

如果你想在每个段落中选择HTML格式,只需将#section {text.text}替换为!