从关系中选择特定字段
Selecting specific fields from a relationship
我正在使用水线(使用 mysql 适配器)在 node.js(帆)上构建一个网络应用程序。
我已经成功地建立了这样的关系:
//Post.js attributes
post_statistics: {
collection: 'postStatistics',
via: 'post'
}
我可以轻松找到关系记录:
var query = {user:userId}; // Just filtering by the user
query.is_public = Post.POSTS_ONLY_ACTIVE; // Only showing active posts, boolean
query.select = ['id','title','content']; // id is only needed for me to get the join working
Post.find(query).populate('post_statistics').exec(cb);
一切正常,但我只需要 post_statistics 中的 select 个特定字段。
这似乎没有任何作用,不过,我希望它能起作用:
Post.find(query).populate('post_statistics',{select:['post','user','id']}).exec(cb);
将字段名称添加到初始 select 更糟糕,因为这些字段已经针对 post table.
有什么明智的方法可以完成这项工作,还是我必须编写整个查询?
目前 .populate({ assoc, select: [] })
在 waterline 中不受支持,https://github.com/balderdashy/waterline/issues/919 中有更多详细信息。
一种可能的替代方法是 运行 通过 .query()
(docs) 的 SQL 查询。
另一种方法是向 waterline 提交补丁,添加对 .populate({ assoc, select: [] })
的支持。
我正在使用水线(使用 mysql 适配器)在 node.js(帆)上构建一个网络应用程序。
我已经成功地建立了这样的关系:
//Post.js attributes
post_statistics: {
collection: 'postStatistics',
via: 'post'
}
我可以轻松找到关系记录:
var query = {user:userId}; // Just filtering by the user
query.is_public = Post.POSTS_ONLY_ACTIVE; // Only showing active posts, boolean
query.select = ['id','title','content']; // id is only needed for me to get the join working
Post.find(query).populate('post_statistics').exec(cb);
一切正常,但我只需要 post_statistics 中的 select 个特定字段。 这似乎没有任何作用,不过,我希望它能起作用:
Post.find(query).populate('post_statistics',{select:['post','user','id']}).exec(cb);
将字段名称添加到初始 select 更糟糕,因为这些字段已经针对 post table.
有什么明智的方法可以完成这项工作,还是我必须编写整个查询?
目前 .populate({ assoc, select: [] })
在 waterline 中不受支持,https://github.com/balderdashy/waterline/issues/919 中有更多详细信息。
一种可能的替代方法是 运行 通过 .query()
(docs) 的 SQL 查询。
另一种方法是向 waterline 提交补丁,添加对 .populate({ assoc, select: [] })
的支持。