如何从外表中选择特定属性

时间:2014-08-29 15:51:03

标签: javascript sequelize.js

假设我们将两个模型定义为发布标记帖子标记通过多对多关联关联。在Sequelize中,我想获得所有帖子,以及所有相应的标签。另外,我正在尝试减少从查询中选择的数据,也就是说,在表格加入后我只需要Tag.id。

我尝试了以下内容:

var find = {
    attributes: [
        'id',
        'text',
        'createdAt'
    ],
    include: [
        {
            model: Tag,
            attributes: [
                'id'
            ]
        }
    ]
};

Post.findAll(find).complete(function(err, data){
    console.log(data);
});

不幸的是,这会返回包含额外属性的JSON,例如" postsTag"加入表格信息......

[{
    "id": 1,
    "text": "HI",
    "createdAt": "2014-08-29T15:41:33.000Z",
    "tags": [{
        "id": 1,
        "postsTag": {
            "createdAt": "2014-08-29T15:41:33.000Z",
            "updatedAt": "2014-08-29T15:41:33.000Z",
            "TagId": 1,
            "PostId": 1
        }
    }, {
        "id": 2,
        "postsTag": {
            "createdAt": "2014-08-29T15:41:33.000Z",
            "updatedAt": "2014-08-29T15:41:33.000Z",
            "TagId": 2,
            "PostId": 1
        }
    }]
}]

最终,我想要以下内容:

[{
    "id": 1,
    "text": "HI",
    "createdAt": "2014-08-29T15:41:33.000Z",
    "tags": [{
        "id": 1 
    }, {
        "id": 2
        }
    }]
}]

任何有关如何实现这一目标的建议都会受到很多关注!我确信我从阅读Sequelize文档中遗漏了一些重要信息。

0 个答案:

没有答案